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.
精彩评论