-->
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.  [ 7 posts ] 
Author Message
 Post subject: no lazy loading for foreign-key based to-one associations
PostPosted: Thu Apr 27, 2006 9:40 am 
Newbie

Joined: Wed Feb 25, 2004 10:43 am
Posts: 14
Hello,

I'll give this another try, as it is really crucial, and I havent found a solution in any of the docs I can access (HTML reference, HIA book):

I have 2 classes A and B that are related through a one-to-one association, which is mapped to a foreign key belonging to B's table. I am doing a query that retreives all instances of class A. When I look at the generated SQL, I see that for each A, another statement is issued to get the related B - leading to the infamous N+1 problem with possibly extreme amounts of redundant SQL being generated.

Since I do not access the A->B references in my particular case, I tried to modify the mapping such that the A->B reference would be resolved lazily. However, I seem to be unable to do away with the redundant SQL, no matter what I try.
I found a section in the docs which states that proxying (which is required for lazy-loading unary references) is only possible if the constrained attribute is set to "true". However, if I understand the constrained attribute correctly, it signifies the side that owns the FK. The conclusion would then be that FK-based associations cannot be handled lazily on the side that does not own the FK - which would be a very disturbing restriction.

Heres my config:
Hibernate version: 3.1.2

mapping A:
one-to-one name="b" lazy="proxy" property-ref="a"

mapping B:
many-to-one name="a" column="a_id" update="false" unique="true" not-null="true"

Query:
from A a

thanks,
Christian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 7:57 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
This problem should go away if you change your one-to-one to many-to-one unique="true". The two mappings are almost, but not quite, equivalent: I use the rule that one-to-one should be used only if you're trying to use the id generator "foreign". I know that it's possible to use it for other reasons, but I don't think that you should: the old requirement that one-to-one associations be primary-key to primary-key was a good one and should never have been removed, IMO.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 4:35 am 
Newbie

Joined: Wed Feb 25, 2004 10:43 am
Posts: 14
Sounds reasonable, I'll give thath a try. I would however definitely call it a non-intuitive solution that should at least be documented someplace. Even more so since the mappings as I had them, though innocently looking, caused unacceptable runtime behavior. After all, hibernate claims to not be plagued by the N+1 problem, and in this case falls right into it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 11:48 am 
Newbie

Joined: Wed Feb 25, 2004 10:43 am
Posts: 14
I have tried your recommendation, but it indeed does neither adress nor solve my problem. Setting the mapping to many-to-one makes hibernate assume the foreign key column is on the A side, which it is not. As I said above, the FK is on the B side.

It seems after all my conclusion must be that foreign-key-based one-to-one associations cannot be configured such that the side that does not own the FK would support lazy loading. Quite disappointing

thanks anyway


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 30, 2006 5:41 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you use many-to-one unique="true" on both sides, the way to define which "side" owns the FK is by using the insert="false", update="false" and optionally cascade="???" attributes. This is one of the other advantages to using one-to-one, as it allows Hibernate to determine which side is the master without extra configuration.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 4:53 am 
Newbie

Joined: Wed Feb 25, 2004 10:43 am
Posts: 14
well, my understanding was that the property-ref attribute would tell which side did (not) own the FK. So, now you offer me 3 different attributes to choose from. How should I decide? The docs do not mention these (at least not where FK-based one-to-one relationships are described), and they dont sound like they will influence the SQL generated by a query.

I am really reluctant to go into another half-day round of experiments - I have a paying customer on my back.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 9:14 am 
Newbie

Joined: Wed Feb 25, 2004 10:43 am
Posts: 14
I'll add one more comment to make the problem more clear.

If you follow the directions given in the hibernate reference under
Code:
7.4.2. one to one
A bidirectional one-to-one association on a foreign key is quite common.

and thereafter do a "from Address a" query, you will see that for each address retreived through the query, another statement is issued to retreive the associated person. Now, if you have a large number of addresses, and you are only interested in the data from the address objects themselves, this can become a serious performance problem.
If, for example you have 10000 addresses, you will see 10000+1 statements issued. Now go tell your DBA "ist my persistence framework that forces me to do this", and see what he says.

I'd be really grateful if somebody could point out a configuration that leaves both model and database schema intact as in the example, and allows lazy retreival of the person association when loading the addresses

thanks,
Christian


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.