Hello,
I have a little problem using the isEmpty() restriction of the criteria api. Maybe anybody can help.
Hibernate version:
3.2.0 but could also be reproduce the issue with 3.1.3
Mapping documents:
Lets say there are three fruit classes like this:
Code:
<class name="Fruit" table="FRUIT">
[...]
<many-to-one name="basket"
class="FruitBasket"
column="BASKET_ID"
cascade="none"
not-null="true"
update="false"
lazy="false"
fetch="join"/>
<subclass name="Apple" discriminator-value="apple">
[...]
</subclass>
<subclass name="Pear" discriminator-value="pear">
[...]
</subclass>
<subclass name="Plum" discriminator-value="plum">
[...]
</subclass>
</class>
Then we have a FruitBasket class:
Code:
<class name="FruitBasket" table="FRUIT_BASKET">
[...]
<set name="apples" inverse="true" cascade="none" lazy="true" where="TYPE='apple'">
<key column="BASKET_ID"/>
<one-to-many class="Apple"/>
</set>
<set name="pears" inverse="true" cascade="none" lazy="true" where="TYPE='pear'">
<key column="BASKET_ID"/>
<one-to-many class="Pear"/>
</set>
<set name="plums" inverse="true" cascade="none" lazy="true" where="TYPE='plum'">
<key column="BASKET_ID"/>
<one-to-many class="Plum"/>
</set>
</class>
Code between sessionFactory.openSession() and session.close():What I want to do now is a criteria query like this:
Code:
List list = session.createCriteria(FruitBasket.class).add( Restrictions.isNotEmpty("pears") ).list;
So I want to get all fruit baskets that have pears in them.
The generated SQL (show_sql=true):That is the problem. What Hibernate generates is a clause like this:
Code:
...where exists (select 1 from FRUIT where this_.ID=BASKET_ID)
So I am not only getting the fruit baskets with pears, but all that have any fruit at all in them. Since the arbitrary sql where condition from my FruitBasket Mapping document is ignored it does not really matter if the isNotEmpty restriction is true for the pears collection. I am getting all Objects where the isNotEmpty restriction is true for any of the tree collections (apples, pears and plums).
Name and version of the database you are using:
PostgreSQL 7 if it does matters.
Any suggestions? Of course I can work my way arround it somehow, but it would be much nicer working with the isNotEmpty restriction. Could this be a bug in Hibernate or am I doing something wrong?
Thanks for your help
Axel