Hibernate Criteria join query one to many -


i have cat class , owner class. cat has 1 owner owner can have many cats. want query "get owners has cat blue eyes".

class cat{ owner owner; //referenced owner.id string eyecolor; }  class owner{ list<cat> catlist; } 

i tried codes don't know do.

criteria criteria = getcurrentsession().createcriteria(cat.getclass(), "cat"); criteria.createalias("cat.owner", "owner");     criteria.add(restrictions.eq("cat.eyecolor", "blue"); 

criteria can select projections, or root entity. not joined entity. queries impossible express criteria (which 1 more reason use hql, in addition better readability , conciseness).

all not lost here, though, because association bidirectional. need equivalent of hql query

select distinct owner owner owner  join owner.cats cat  cat.eyecolor = 'blue' 

which is

criteria c = session.createcriteria(owner.class, "owner"); c.createalias("owner.cats", "cat"); c.add(restrictions.eq("cat.eyecolor", "blue"); c.setresulttransformer(criteriaspecification.distinct_root_entity); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -