-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Criteria query Collections contains certain elemen
PostPosted: Thu Jan 08, 2015 10:58 am 
Newbie

Joined: Thu Jan 08, 2015 10:51 am
Posts: 2
I have some problems with Criteria.I have a Job class that contains a set of Skills.

The problem comes when i want to filter jobs that contains 2 skills for example(all jobs that contains skill with id 1 and 3).
USING:

Code:
for(Skill skill:job.getSkills()){
                    ids.add(skill.getId());
}

criteria.createAlias("skills", "skill");
criteria.add(Restrictions.in("skill.id", ids));


it gives me jobs that contains skill 1 or 3,not only those with both skills.

USING:
Code:
   Conjunction and = Restrictions.conjunction();   
         for(Skill skill:job.getSkills()){
            and.add(Restrictions.eq("skill.id",skill.getId()));
   
         }
         criteria.add(and);


it returns me nothing and the query looks like :
Code:
and (skill1_.ID=? and skill1_.ID=?)
same skill alias.

Please give me some ideas.Thanks


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria query Collections contains certain elemen
PostPosted: Mon Jan 12, 2015 10:58 am 
Newbie

Joined: Thu Jan 08, 2015 10:51 am
Posts: 2
This is the solution.



Code:
for(Skill skill:job.getSkills()){
                    DetachedCriteria subquery = DetachedCriteria.forClass(Skill.class,"skill");
                    subquery.add(Restrictions.eq("id",skill.getId()));
                    subquery.setProjection(Projections.property("id"));
                    subquery.createAlias("jobs", "job");
                    subquery.add(Restrictions.eqProperty("job.id", "Job.id"));
                    criteria.add(Subqueries.exists(subquery)); 
                }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.