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;