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.