Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Using Criteria API for many-to-many search

 
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.
 
Author
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.
 
You can't expect to wield supreme executive power just because
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic