• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hibernate Criteria query: finding matching records in a ManyToMany relationship

J. Cartwright
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?



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

Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!