1

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

See, now that I missed! But then it is not possible for a referring instance to specify that the referred association should be eagerly fetched? It is a matter of defining it one for all, or none at all? But thanks! It most certainly worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.