Hibernate lazy loading + Jersey REST = eager loading?
I'm developing a Client-Server-Application with Hibernate as persistence layer and Jersey REST for network communication.
Given a user with roles:
- when I want to display all users in the client, I don't want the roles to be loaded by Hibernate and I don't want them to be sent over the network
- when I want to modify the user's roles, I want them to be loaded and to be transferred
I'm using a preloading pattern (http://bwinterberg.blogspot.com/2009/09/hibernate-preload-pattern.html), to determine which properties should be loaded by Hibernate. This works fine.
In case 1, no roles are loaded by Hibernate, just as intended. But as soon as Jersey creates the XML to be sent to the client, it reads the user's roles, which in turn lets Hibernate load the roles (and all other properties). In the end, Hibernate always loads the complete tree of datasets belonging to a user.
I thought about detaching the user by closing the session before I pass the user to Jersey, so Hibernate can't load the roles, but that doesn't seem to have any effect.
Any ideas?
make a shallow clone of your entity and set an empty collection to it, before giving it for serialization
make a custom proxy of the proxied collection, and delegate the loading only in some circumstances (the first usecase), and return an empty collection in other
精彩评论