-->
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.  [ 4 posts ] 
Author Message
 Post subject: one-to-one n+1 SQL query problem
PostPosted: Thu May 06, 2004 4:28 pm 
Newbie

Joined: Thu May 06, 2004 4:25 pm
Posts: 13
Location: Prague, Czech Republic
I've got a little one-to-one problem.
I've got two classes with one-to-one relationship. For example: User and ContactInfo. Classes are bound together with primary keys. Relationship is uni-directional to be only possible to call user.getContactInfo(). Mapping for User looks as follows:

Code:
<one-to-one name="contactInfo" cascade="all" constrained="true" class="ContactInfo"/>


I've set outer join fetching ON and, so the load of a single user results in one SQL query joining User and ContactInfo tables. That's correct. But if I'd like all users with status=0, this HQL query results in n+1 SQL queries.

Code:
select user
from User as user
where user.status=0


First it selects all Users with status=0 and than selects for each User all data from User joining ContactInfo. If I change the HQL query a little, result is only one SQL query, but the resultset is a Collection of arrays (User and its ContactInfo).

Code:
select user, user.contactInfo
from User as user
where user.status=0

I know I can separate it in Java code, but I think there is much more elegant solution.

Thank for help. Sorry if this is a common problem, I wasn't able to find it here.


Pavel


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 06, 2004 6:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Use left join fetch in your HQL


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 8:59 am 
Newbie

Joined: Fri May 07, 2004 4:34 am
Posts: 5
Is this 'left join fetch' supposed to work with 'key-many-to-one' relationships ? It seems not.
I also have this problem about n+1 SQL queries.

I tried to activate the proxies, but nothing changed.
A solution would be to use a different PK, and change the 'key-many-to-one' to a more simple 'many-to-one' relationship. (I wouldn't like this solution, since the db model would not be as smart and clean as it should be.)

Sadly, there's not much documentation about this key-many-to-one...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 12:30 pm 
Newbie

Joined: Thu May 06, 2004 4:25 pm
Posts: 13
Location: Prague, Czech Republic
Thank you very much. That is exactly what I needed. Now my HQL looks like this:

Code:
   
select user
from User as user left join fetch user.contactInfo


avel


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