java - Hibernate criteria to have multiple restrictions on the child collection -
i have write criteria query clause match both first & last names in child collection. both names in different rows
tried this, not return when matching data present, because it's trying match both restrictions on same row.
criteria criteria = getcurrentsession().createcriteria(form.class); criteria.createalias("responses", "r"); criteria.add(restrictions .conjunction() .add(restrictions.eq("r.id", "firstname")) .add(restrictions.eq("r.value", getfirstname()))); criteria.add(restrictions .conjunction() .add(restrictions.eq("r.id", "lastname")) .add(restrictions.eq("r.value", getlastname())));
tried , gives exception org.hibernate.queryexception: duplicate association path: responses
criteria criteria = getcurrentsession().createcriteria(form.class); criteria.createalias("responses", "r1"); criteria.createalias("responses", "r2"); criteria.add(restrictions .conjunction() .add(restrictions.eq("r1.id", "firstname")) .add(restrictions.eq("r1.value", getfirstname()))); criteria.add(restrictions .conjunction() .add(restrictions.eq("r2.id", "lastname")) .add(restrictions.eq("r2.value", getlastname())));
any help?
edit
it looks description question wasn't clear. here basic requirement:
query records form class have (a child response record id=firstname , value=somename1) , (a child response record id=lastname , value=somename2)
i adding solution worked me, using subqueries. not sure if best way, solved problem
to query or :
criteria criteria = getcurrentsession().createcriteria(form.class); criteria.createalias("responses", "r"); junction conditiongroup = restrictions.disjunction(); conditiongroup.add(restrictions .conjunction() .add(restrictions.eq("r.id", "firstname")) .add(restrictions.eq("r.value", getfirstname()))); conditiongroup.add(restrictions .conjunction() .add(restrictions.eq("r.id", "lastname")) .add(restrictions.eq("r.value", getlastname()))); criteria.add(conditiongroup);
Comments
Post a Comment