one-to-many with criteria question

Posted by brnzn on Stack Overflow See other posts from Stack Overflow or by brnzn
Published on 2009-07-13T14:31:15Z Indexed on 2010/06/08 6:02 UTC
Read the original article Hit count: 215

Filed under:
|
|

enter code hereI want to apply restrictions on the list of items, so only items from a given dates will be retrieved.

Here are my mappings:

    <class name="MyClass" 
            table="MyTable" mutable="false" >
            <cache usage="read-only"/>
    <id name="myId" column="myId" type="integer"/>
    <property name="myProp" type="string" column="prop"/>
    <list name="items" inverse="true" cascade="none">
    	<key column="myId"/>
    	<list-index column="itemVersion"/>
    	<one-to-many class="Item"/>
    </list>	
   </class>
    <class name="Item" 
            table="Items" mutable="false" >
            <cache usage="read-only"/>
    <id name="myId" column="myId" type="integer"/>
    <property name="itemVersion" type="string" column="version"/>
    <property name="startDate" type="date" column="startDate"/>
   </class>

I tried this code:

Criteria crit = session.createCriteria(MyClass.class);
crit.add( Restrictions.eq("myId", new Integer(1)));
crit = crit.createCriteria("items").add( Restrictions.le("startDate", new Date()) );

which result the following quires:

select ...
from MyTable this_ inner join Items items1_ on this_.myId=items1_.myId 
where this_.myId=? and items1_.startDate<=?

followed by

select ...
from Items items0_ 
where items0_.myId=?

But what I need is something like:

select ...
from MyTable this_ 
where this_.myId=?

followed by

select ...
from Items items0_ 
where items0_.myId=? and items0_.startDate<=?

Any idea how I can apply a criteria on the list of items?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about criteria