开发者

Hibernate lazy loading + Jersey REST = eager loading?

开发者 https://www.devze.com 2022-12-20 04:19 出处:网络
I\'m developing a Client-Server-Application with Hibernate as persistence layer and Jersey REST for network communication.

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?


  • 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

0

精彩评论

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

关注公众号