Hi,
I'm experiencing awkward behaviour using compound path expression in the where clause, when I'm placing a restriction on a one-to-one association.
I expected the following HQL query to work the same regardless of whether the database column is on table X or table Y, but this is not the case. As I have now found out, the query "from X as x where x.y is null" will only work as expected if the foreign key column is in table X (or in both--a double one-to-one).
Hibernate version: 3.0.5
Mapping documents:
Code:
<class name="A">
<id name="id">
<generator class="native"/>
</id>
<many-to-one name="b"
class="B"
unique="true"
/>
</class>
<class name="B">
<id name="id">
<generator class="native"/>
</id>
<one-to-one name="a"
class="A"
property-ref="b"
/>
</class>
Code between sessionFactory.openSession() and session.close():testWhereAIsNull()
Code:
// one A and one B
s.persist(new A());
s.persist(new B());
Query query = s.createQuery("from B as b where b.a is null");
assertEquals(1, query.list().size()); // fails: expected <1>, but was <0>!
testWhereBIsNull()
Code:
// one A and one B
s.persist(new A());
s.persist(new B());
Query query = s.createQuery("from A as a where a.b is null");
assertEquals(1, query.list().size());
The generated SQL:testWhereAIsNull()
Code:
select b0_.id as id from B b0_ where b0_.id is null
testWhereBIsNull()
Code:
select a0_.id as id, a0_.b as b0_ from A a0_ where a0_.b is null
The SQL that's generated for testWhereAIsNull() seems to be way off. If I use "is null" it always returns 0 results, and if I use "is not null" it returns every row in the table.
Is this the expected behaviour?