-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Lazy but for to-one association
PostPosted: Fri Mar 19, 2004 10:44 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
excuse me, but i have a stupid question and i just can't remember...

is there a simple way to have the same function as "lazy loading" but for a to-one association.

I know that (simplifying)
outer-join = true will execute on query
outer-join = false will execute x queries

but i can't remember if it is possible not to join until we want it.

example
class A many - to - one class B

i want myA.B to be null until i call myA.getB() (that will generate SQL and hit the DB)


And excuse me again, it seems i'm too busy to remember some basics... :-/

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 11:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This is not possible (Keep something "null" until get is called.) You can use a proxy for the one side class though (take a look at http://www.hibernate.org/162.html it is conceptually the same as for a one-to-one)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 11:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
thank you Michael, that's what i was thinking but i wanted to be sure

Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 2:53 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
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?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 3:10 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
Or I suppose I could add a nullable B_ID column to TBL_A, map the association as a many-to-one with outer-join="false" and proxy B. Seems much less intrusive. Thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 20, 2004 5:46 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
I've got the lazy-loading with the proxy working great with many-to-one


Is it made exactly on http://www.hibernate.org/Documentation/ ... azyLoading?

If no, can you give a piece of code?
Maybe you should had your complete example in a wiki page?
Or if you want just give the code, i'll complete the page.

Thanks
Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 3:36 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
Quote:


Not sure I understand your question. The wiki page explains the problem very well, but doesn't offer any solutions for cases where the proxied object can be null. (Incidentally, I don't understand what that has to do with the "constrained" attribute -- that's my only question regarding the wiki page).

What I have found is that a many-to-one association doesn't have that limitation -- the proxied object can be null, because no extra join is needed -- the key of our -to-one class is stored in a nullable column of the table of our primary class.

Quote:
If no, can you give a piece of code?


Well I haven't tried to build the application-managed lazy loading stuff, but it's fairly straightforward. If I attempt it, I'll post the code and let you know how it turned out.

However, I am leaning towards the second strategy that I posted, which would be to make the relationship many-to-one, rather than one-to-one -- because the many-to-one association allows for the proxied class on the -to-one side to be null, whereas the one-to-one association does not.

If I refactor my tables & mappings as I described, I will let you know how it turns out and post some code.

I was just wondering if anyone else had encountered a similar problem and how they had dealt with it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 10:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
constrained="true" means that there is a foreign key constraint on the pk, and hence that the one-to-one association can not be null ... and therefore we can do lazy loading.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 10:32 am 
Beginner
Beginner

Joined: Mon Nov 03, 2003 11:48 pm
Posts: 29
Gavin, but what about cascading saving as in http://forum.hibernate.org/viewtopic.php?t=932623 and http://forum.hibernate.org/viewtopic.php?t=932541
??


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.