I've been looking around the forum and experimenting to try to do the same thing.
I've got the lazy-loading with the proxy working great with many-to-one, but a one-to-one breaks if the -to-one object is null, as described at
http://www.hibernate.org/Documentation/SomeExplanationsOnLazyLoading.
So I was wondering - has anyone come up with a good programmatic strategy for lazy-loading a one-to-one association that is
not constrained (e.g. doing what Michael said is impossible: keeping the object null)?
Since you cannot proxy the -to-one class (B), seems like the only thing left to do is to remove the one-to-one relationship from the A mapping and then put some lazy-loading in the getB() method of the A class.
for example, in class A I have something like:
Code:
public B getB()
if b is null and I have not tried to load it yet
get the hibernate session that A is associated with and try to load b using a Query
if B was null, set an attribute indicating that load was attempted
return b;
While I'm not thrilled about putting hibernate code into a property getter of my A class, it's what we did back in the JDBC days, and it's the only solution I've thought of. If anyone has better ideas I'd love to hear them.
Also, I was wondering if anyone knew the best way for A to get a reference to the session that loaded/locked it?