Hibernate 延迟加载 + Jersey REST = 急切加载?

发布于 2024-08-22 02:32:41 字数 659 浏览 4 评论 0原文

我正在开发一个客户端-服务器应用程序,其中 Hibernate 作为持久层,Jersey REST 用于网络通信。

给定一个具有角色的用户:

  1. 当我想在客户端显示所有用户时,我不希望 Hibernate 加载这些角色,并且
  2. 当我想修改用户的角色时, 我不希望它们通过网络发送,我希望它们被加载并传输

我正在使用预加载模式(http://bwinterberg.blogspot.com/2009/09/hibernate-preload-pattern.html),以确定 Hibernate 应加载哪些属性。这很好用。

在情况 1 中,Hibernate 没有加载任何角色,正如预期的那样。但是,一旦 Jersey 创建要发送到客户端的 XML,它就会读取用户的角色,这反过来又让 Hibernate 加载角色(以及所有其他属性)。 最后,Hibernate 总是加载属于用户的完整数据集树。

我考虑过在将用户传递给 Jersey 之前通过关闭会话来分离用户,这样 Hibernate 就无法加载角色,但这似乎没有任何效果。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-08-29 02:32:41
  • 在进行序列化之前,对实体进行浅克隆并为其设置一个空集合

  • 创建被代理集合的自定义代理,仅在某些情况下(第一个用例)委托加载,并在其他情况下返回空集合

  • 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文