-->
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.  [ 3 posts ] 
Author Message
 Post subject: One-to-one relationships and number of generated SELECTs
PostPosted: Mon Jun 28, 2004 3:27 pm 
Newbie

Joined: Mon Jun 28, 2004 12:47 pm
Posts: 2
Hello Hibernaters,

I've got a newbie question about one-to-one mappings. Specfically, about the SQL that Hibernate generates when retrieving rows from tables with one-to-one relationships.

Let's say that I have two tables: A and B. They have a one-to-one relationships. Table B has a FK to A. So the mappings are:
Code:
<class name="example.A" table="A" lazy="false">
    <id name="objId" type="long" column="objId">
        <generator class="increment"/>
    </id>
    <property name="text" column="A_Text" type="string"/>
    <one-to-one name="b" class="example.B" property-ref="a"/>   
</class>

<class name="example.B" table="B" lazy="false">
    <id name="objId" type="long" column="objId">
        <generator class="increment"/>
    </id>
    <property name="text" column="B_Text" type="string"/>
    <many-to-one name="a" class="example.A" column="A_ObjId" unique="true"/>
</class>


Now when I query for rows from A, I want to get the corresponding rows from B as well. I would expect to get a single SQL select statement, like:
Code:
select <cols from A and cols from B> from A join B on B.A_ObjId=A.ObjId


Instead, I get 1+N select statements:
Code:
select <cols from A> from A join B on B.A_ObjId=A.ObjId
select <cols from B> from B where B.A_ObjId=?
.
.
.
select <cols from B> from B where B.A_ObjId=?


Notice that I've used (or tried to use) the "unique foreign key associations" mapping approach suggested in the manual. When I switch to using the "primary key associations" approach, I do get a single select statement.

Now for my question..... is it possible to get a single select statement when using the "foreign key associations" approach to mapping one-to-one relationships?

Thanks,

Mike

PS: there is a post that deals with the difference between primary key associations and unique FK associations. Here: http://forum.hibernate.org/viewtopic.php?t=928211. This post, however, deals with lazy instantiation, rather than join behaviour.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:29 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
take a look at FETCH

from A as a fetch join a.b should work

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 6:23 pm 
Newbie

Joined: Mon Jun 28, 2004 12:47 pm
Posts: 2
Anthony,

I thought that FETCH would work, too. But it does not seem to help. Here's the HQL that I'm using:
Code:
Query q = session.createQuery("select a from A as a join fetch a.b");


(Note that this is not identical to your example.... but the snippet that you gave resulted in an error.)

So.... we're still stuck. Other thoughts?

Mike


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