禁用 Hibernate 延迟获取多对一关联

发布于 2024-12-07 03:56:37 字数 911 浏览 3 评论 0原文

我之前看到过许多问题的主题,但与这个主题不完全一样。我了解 Hibernate 的延迟获取策略、视图中打开会话等,但我似乎无法找到这个简单的小事情的解决方案,无论如何,我的关联都是延迟获取的。

我没有在视图中使用开放会话,因为我的网络应用程序是在没有它的情况下构建的,现在“迁移”变得复杂......

假设我们有一个引用链,比如 a <- b a <- b ;-c<-d->;-c<-d-> e
通常,获取a也意味着我们需要b、cd。因此,这些被设置为始终急切地获取(通过在 hibernate 映射文件中指定lazy=false)。这有效!

然而,我们现在还需要在每次获取 d 时始终急切地获取关联 e。由于e对于在d上执行一些逻辑运算至关重要。

这就是它停止工作的地方... ed 关联为 d 上的多对一关联。将其设置为 lazy=false 并不能解决问题,它仍然没有正确初始化,并且从 d 访问 e 上的属性会导致 LazyInitializationException 。

我期望这会起作用是错误的吗?使用 hibernate v. 3.2.6 和 Spring 版本 2.5.6 在 hbm 文件中指定惰性属性。

希望有人能为我澄清事情...

无论如何,如果您找到了另一篇文章,请参阅另一篇文章。我似乎找不到涵盖这一点的人...

A topic of many previous questions I see - but not quite as this one however. I understand Hibernates lazy fetching strategies, open-session-in-view etc., but I cant seem to find the solution to this simple little thing, where my association is fetched lazily no matter what.

I am not using open session in view, as my web-app was build without it, and is now to complex to "migrate"...

Let's say we have a chain of references, say a <- b <- c <- d -> e
More often than not, fetching a also means we need b, c and d. So these are set to be always eagerly fetched (by specifying lazy=false in the hibernate mapping file). This WORKS!!

However, we now also need to always eagerly fetch the association e, everytime d is fetched. Since e is crucial to perform some logic operations on d.

And this is where it stops working... e is associated with d as a many-to-one association on d. Setting this to lazy=false doesn't do the trick, it is still not properly initialized, and accessing properties on e from d causes LazyInitialisationException.

Is it wrong of me to expect that this would work? The lazy property is specified in the hbm files using hibernate v. 3.2.6 and Spring version 2.5.6.

Hope someone can clarify things for me...

By all means, please refer to another post if you find one. I cant seem to find one covering this...

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

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

发布评论

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

评论(1

水中月 2024-12-14 03:56:37

多对一关联的惰性属性在类元素中设置:

<class name="E" laze="false">
...
</class>

<class name="D">
  <many-to-one name="e" .../>
</class>

多对一关联的惰性属性与任何属性具有相同的含义:它允许单个属性的延迟加载,仅当所有者本身是代理时才有效。这在你的情况下是不能使用的,并且默认情况下它被设置为 false。

The lazy attribute of many-to-one associations is set in the class element:

<class name="E" laze="false">
...
</class>

<class name="D">
  <many-to-one name="e" .../>
</class>

The lazy attribute on many-to-one has the same meaning as for any property: it allows lazy loading of single properties, which only works when the owner itself is a proxy. This is nothing you can use in your case and it is set to false by default anyway.

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