开发者

Disable Hibernate lazy fetching many-to-one associations

开发者 https://www.devze.com 2023-04-08 02:44 出处:网络
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

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...


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.

0

精彩评论

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