views:

390

answers:

1

I'm developing a Client-Server-Application with Hibernate as persistence layer and Jersey REST for network communication.

Given a user with roles:

  1. 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
  2. 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?

+1  A: 
  • 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

Bozho
Thanks! Gonna try that on monday.
Olvagor
Okay, I tried making a shallow copy and setting an empty collection. That worked fine. But it also worked without the copy, because fortunately the change is not committed to the database.Now I set my empty collection directly to the entity from database. Thanks for your hint!
Olvagor