Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

public class Book {
   private String bookId;
   private String author;
   private String isbn;
   private Set<Tag> tags;
   // getters, setters etc.
}

and

public class Tag  {
   private String tagId;
   private String tagName;
  // getters, setters, etc.
}

There is a many-to-many association between the two that is represented by a join table books_tags_mn with the columns book_id and tag_id.

What I like to do is the following: I want to create a hibernate query/criteria query that returns all book that have all of a certain set of tags. What does work is to select all books that have any of a set of tags.

I've been messing around with the criteria API but did not truly understand it. So what I am trying to do (in pseudo HQL)

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

Any help on this would be highly appreciated, so thank you very much in advance.

like image 433
Stephan Schobloch Avatar asked Oct 08 '12 15:10

Stephan Schobloch


People also ask

How to get uniqueResult in Hibernate Criteria?

Fetch a Single Result If you want to obtain a single Object reference instead of a List, the uniqueResult() method on the Criteria object returns an object or null. If there is more than one result, the uniqueResult() method throws a HibernateException .

What is the Criterion query in Hibernate?

Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.

How to add Restrictions in Criteria query?

Syntax of createCriteria() method of Session interface public Criteria add(Criterion c) is used to add restrictions. public Criteria addOrder(Order o) specifies ordering. public Criteria setFirstResult(int firstResult) specifies the first number of record to be retreived.

How do I get all entities from a table in hibernate?

JPQL JPQL provides a simple and straightforward way to get all entities from a table. Let's see what it might look like to retrieve all students from a table using JPQL: Our Hibernate session's createQuery () method receives a typed query string as the first argument and the entity's type as the second.

How to store a list of values as an entity attribute?

You can use the @ElementCollection annotation to store a list of values as an entity attribute without needing to model an additional entity. That might look like a great feature, but it has a few downsides, as I explained in a previous Hibernate Tip.

How do I query for phone numbers in hibernate?

Hibernate generates the following SQL statement for both queries. Even so, the phone numbers are not modeled as a separate entity; Hibernate maps them to the Author_phoneNumbers table. That’s why you need to join them in your query before you can reference the elements in the WHERE clause. phonenumbe1_.phoneNumbers=?

What is author_class in hibernate?

The Author_ class is part of the JPA metamodel and enables you to reference the attributes of the Author entity in a typesafe way. I explained it in more details in Create type-safe queries with the JPA static metamodel. Hibernate generates the following SQL statement for both queries.


1 Answers

You could use the following query:

select book from Book book
where :numberOfTags = (select count(tag.id) from Book book2
                       inner join book2.tags tag
                       where book2.id = book.id
                       and tag in (:tags))

where numberOfTags is the number of tags in the set of tags that must be matched.

like image 143
JB Nizet Avatar answered Oct 10 '22 12:10

JB Nizet