-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Criteria search against many-to-many set property?
PostPosted: Thu Jun 25, 2009 8:58 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
How do I set a restriction against an entity where the property is a set of subordinate classes?

In this case I have a parent entity 'FullExpertise' that has a set property consisting of one or more 'Keyword' instances. My search is to find all the 'FullExpertise' instances that have a link to a Keyword with an nominated keyword ID, like...

Code:
        public FullExpertise[] findByKeyword(int aKeywordID);


This is the higher level entity mapping...
Code:
    <class name="com.eis.knowledge.expertise.search.FullExpertise" table="expertise">
        <id name="ID" type="java.lang.Integer" unsaved-value="-1">
            <column name="ExpertiseIntID" />
            <generator class="native" />
        </id>
         ....
         ....
        <set name="keywords" inverse="true" table="expertise_keyword_link">
            <key>
                <column name="ExpertiseIntID" not-null="true" />
            </key>
            <many-to-many column="EntityKeywordIntID" class="com.eis.keyword.Keyword" order-by="EntityKeywordTitle"/>
        </set>
    </class>



This is the lower level 'Keyword' entity mapping...
Code:
    <class name="com.eis.keyword.Keyword" table="entity_keyword" >
        <id name="ID" type="java.lang.Integer" unsaved-value="-1">
            <column name="EntityKeywordIntID" />
            <generator class="native" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="EntityKeywordTitle" length="100" not-null="true" />
        </property>
    </class>



Any ideas?


Top
 Profile  
 
 Post subject: Re: Criteria search against many-to-many set property?
PostPosted: Thu Jun 25, 2009 9:15 am 
Newbie

Joined: Thu Jun 25, 2009 7:22 am
Posts: 9
Once I had the same problem. Try this HQL query:
Code:
select fe from FullExpertise fe join fe.keywords as keyword where keyword = ?

and set the keyword ID via setParameter.


Top
 Profile  
 
 Post subject: Re: Criteria search against many-to-many set property?
PostPosted: Thu Jun 25, 2009 9:21 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
For various reasons I have to use a Criteria based search. Could you help with a translation of your suggested HQL to a set of Criteria based code lines?


Top
 Profile  
 
 Post subject: Re: Criteria search against many-to-many set property?
PostPosted: Thu Jun 25, 2009 9:23 am 
Newbie

Joined: Tue Jun 23, 2009 3:31 am
Posts: 10
You can try this...

Code:
Criteria criteria = session.createCriteria(FullExpertise.class)
                                 .createCriteria("keywords")
                                 .add(Restrictions.eq("id", aKeywordID));
List<FullExpertise> list = criteria.list();


Top
 Profile  
 
 Post subject: Re: Criteria search against many-to-many set property?
PostPosted: Sat Aug 08, 2009 6:29 am 
Newbie

Joined: Sat Nov 26, 2005 4:32 am
Posts: 8
Hello,

I am facing the same situation but the code above does not solve the problem. It seems that the cretiria applied to keywords does not restrict the result set. Any ideas?

thanx
/axelmangr


Top
 Profile  
 
 Post subject: Re: Criteria search against many-to-many set property?
PostPosted: Sat Aug 08, 2009 7:42 am 
Newbie

Joined: Sat Nov 26, 2005 4:32 am
Posts: 8
Sorry about my previous post. The suggested solution works as a charm. I had mixed the ids.

thanx
/axelmangr


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.