Hi folks,
I face a strange result when using setEntity in my queries when using bidirectional one to one association.
I have reduced my probleme to the follow testcase :
A [1]< -----------> [0..1] B
Mapping file :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="debug">
<class name="A">
<id name="id" column="a_id">
<generator class="native"/>
</id>
<property name="name"
type="string"
not-null="true"/>
<many-to-one name="b"
column="b_id"
unique="true"
not-null="false"/>
</class>
<class name="B">
<id name="id" column="b_id">
<generator class="native"/>
</id>
<property name="name"
type="string"
not-null="true"/>
<one-to-one name="a"
property-ref="b"/>
</class>
</hibernate-mapping>
(name attribute just to be clearer)
My test data :
Code:
dbtest=# select * from a;
a_id | name | b_id
------+------+------
3 | a3 |
1 | a1 | 5
2 | a2 | 6
(3 lignes)
dbtest=# select * from b;
b_id | name
------+------
4 | b1
5 | b2
6 | b3
(3 lignes)
dbtest=#
Particularly, I have a2 <---------> b3
Trouble is, I am able to get A from B using :
Code:
List result = session.createQuery("from A a where a.b = ?").setEntity(0, b3).list();
I get a list containing a2.
But the other direction does not work :
Code:
List result = session.createQuery("from B b where b.a = ?").setEntity(0, a2).list();
(empty list returned instead of a list containing b3)
But this seems to be related to setEntity as other queries are working as expexted :
Code:
List result = session.createQuery("from B b where b.a.name = ?").setParameter(0, "a2").list();
List result = session.createQuery("from B b inner join fetch b.a where b.a.id = ?").setParameter(0, a2.getId()).list();
Am I doing something wrong ?
Many thanks...
Quentin