• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Criteria API for many-to-many search

 
Ben Gateau Jr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I hope someone can help me with using Criteria API to create correct search query. I have many-to-many relationship between Clients and Products. On the search page I have list of all the products,
and when user selects some products, I would like query to return all the clients who have ALL the products selected by the user. For example, on the list we have ProductOne, ProductTwo, ProductThree, and
if the user selects ProductOne and ProductThree, I'd like all the clients who have (among the possible others) ProductOne and ProductThree.
At the moment I have a query which returns clients which have either of the two products, but I want only those who have both.



I understand that 'Restrictions.in' returns true for each given product, but I don't know how to rewrite it and what to used instead in order to return true only if Client has all the given products.

Regards, Ben.
 
Ben Gateau Jr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone have any idea how to achieve this? I had high hopes for the Restrictions.conjunction() but it doesn't work well when the same field is used. Any links or recommended books would be appreciated too.

Regards, Ben.
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe "in" is like stringing ORs together. You may just have to string ANDs together for each product.
 
Ben Gateau Jr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javid,

Exactly. I have found the solution here.

Regards, Ben.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic