• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Criteria query: finding matching records in a ManyToMany relationship

 
J. Cartwright
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a ManyToMany relationship between Content and Keyword entities (one Content instance contains a List of Keyword references). I'd like to find Content instances whose list of Keywords contains one or more of the Keywords in a provided list.

I can accomplish this with the following Criteria query if I provide a list of Keyword IDs rather than Keyword instances. It seems like there should be a way to be able to use the list of Keywords directly rather than have to create a separate list of IDs from it. Can someone please help me out?

Thanks!

--john

Criteria criteria = getSession().createCriteria(Content.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Criteria keywordCriteria = criteria.createCriteria("keywords");
keywordCriteria.add(Restrictions.in("id", ids));

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic