Hibernate: Select entities where collection contains all of the specified valus

前端 未结 3 1730
闹比i
闹比i 2020-12-16 06:31

I need assistance with a tricky hibernate query problem. I have the following entities:

public class Book {
   private String bookId;
   private String autho         


        
3条回答
  •  清酒与你
    2020-12-16 07:00

    I would advice you to create two custom functions or restriction as :

     collect(book.tags) -> returns list of tags associated with the book
    
     containsAll(bookTagsList, tags) --> validates and returns true if all 
                                         tags elements are present in bookTagsList 
                                         returned by the first function "collect"
    

    Once functions are defined and registered, you would be able to run HQL/criteria query like:

     from Book book where containsAll(collect(book.tags), :tags)
    

    or

    session.createCriteria(Book.class).add(
               Restrictions.add(collect("tags").containsAll(tags))
            ).list();
    

    Please Note: This is just a sample pseudo code to share the idea.

    Hope this helps!

提交回复
热议问题