开发者

Cleanse Hibernate from my object but don't lazy load

开发者 https://www.devze.com 2023-03-16 12:55 出处:网络
I have an JPA+Hibernate entity that I need to send via RMI to a client that doesn\'t know Hibernate, so I\'ve made a method to \"cleanse\" Hibernate from it:

I have an JPA+Hibernate entity that I need to send via RMI to a client that doesn't know Hibernate, so I've made a method to "cleanse" Hibernate from it:

// shortened
public class Player {
   priv开发者_如何学运维ate Set<Item> ownedItems;
   public void makeSerializable() {
      ownedItems = new HashSet<Item>(ownedItems);
   }
}

However, when I call makeSerializable Hibernate will attempt to lazy-load ownedItems if it's not loaded yet, which I don't want, and which is also impossible because there is Hibernate session. Instead, if ownedItems is not loaded, I'd like to set it to null or an empty set.

How can I do that?


if (!Hibernate.isInitialized(ownedItems)) {
    ownedItems = new HashSet<Item>();
}

This is the way to test if a collection is initialized without the need for a session.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号