-->
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: SELECT on a ONE-TO-ONE mapping doesn't work as expected
PostPosted: Mon Apr 19, 2004 9:28 am 
Newbie

Joined: Tue Mar 02, 2004 8:35 am
Posts: 6
Why does the select of a one-to-one mapping doesn't work if I ask only for the elements from one side where on the other side the element doesn't exist. The two selects A) and B) show in more detail my problem and as well you can find the Left & Right example below.

Select A): Selecting all Left where a Right exist.
lListOfAktions =
Code:
lSession.find("from Left as lft where lft.right is not null");
-> select left0_.ID_LEFT as ID_LEFT
from EN2DBETA.LEFT left0_
where (left0_.ID_LEFT is not null)
-> This select does the where on the primary key of Left instead of Foreign Key of Right. I would have expected a join and a WHERE right.FK_LEFT is not null.


Select B): Selecting all Left where a Right exist (incl join).
lListOfAktions = lSession.find("from Left as lft left join lft.right as rgt where rgt.left is not null");
-> select left0_.ID_LEFT as ID_LEFT0_, right1_.ID_RIGHT as ID_RIGHT1_, right1_.FK_LEFT as FK_LEFT1_
from EN2DBETA.LEFT left0_
left outer join EN2DBETA.RIGHT right1_ on left0_.ID_LEFT=right1_.FK_LEFT
where (right1_.FK_LEFT is not null )
-> This select returns a list of an object array of Left/Right objects. I would have expected only a list of Left objects.


Mapping File for entity Left:
Code:
<class name="persistency.struct.Left" table="LEFT">
    <id name="idLeft" type="java.lang.Integer" column="ID_LEFT">
        <generator class="assigned" />
    </id>
   
   <one-to-one name="right"
                      class="persistency.struct.Right"
                      property-ref="left" />
</class>


Mapping File for entity Right:
Code:
<class name="persistency.struct.Right" table="RIGHT">
    <id name="idRight" type="java.lang.Integer" column="ID_RIGHT">
        <generator class="assigned" />
    </id>
    <many-to-one name="left"
             class="persistency.struct.Left"
             column="FK_LEFT"
             not-null="true"
             unique="true" />
</class>


DDL for tables Left & Right:
Code:
CREATE TABLE EN2DBETA.RIGHT (
   ID_RIGHT INTEGER NOT NULL,
   FK_LEFT INTEGER NOT NULL,
   CONSTRAINT PKRIGHT PRIMARY KEY (ID_RIGHT)
);

CREATE TABLE EN2DBETA.LEFT (
   ID_LEFT INTEGER NOT NULL,
   CONSTRAINT PKLEFT PRIMARY KEY (ID_LEFT)
);

ALTER TABLE EN2DBETA.RIGHT ADD CONSTRAINT FKUNIQUE UNIQUE (FK_LEFT);
ALTER TABLE EN2DBETA.RIGHT ADD CONSTRAINT FKLEFT FOREIGN KEY (FK_LEFT) REFERENCES EN2DBETA.LEFT (ID_LEFT) ON DELETE RESTRICT;


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 10:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
B: Use "seleft lft from Left as lft ..."


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 10:39 am 
Newbie

Joined: Tue Mar 02, 2004 8:35 am
Posts: 6
SELECT C) WORKS FINE, AS YOU SAID. THANKS!!
lListOfAktions = lSession.find
Code:
("select lft from Left as lft left join lft.right as rgt where rgt.left is not null");
-> select left0_.ID_LEFT as ID_LEFT from EN2DBETA.LEFT left0_ left outer join EN2DBETA.RIGHT right1_ on left0_.ID_LEFT=right1_.FK_LEFT where (right1_.FK_LEFT is not null )


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 10:48 am 
Newbie

Joined: Tue Mar 02, 2004 8:35 am
Posts: 6
One last question

One point I still don't understand is why "Select A): Selecting all Left where a Right exist. " doesn't work at all, as I would expect it?

Why hibernate does the WHERE with the primary key of left instead of the foreign key of right to left?

[/list]


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.