Skip to content

Instantly share code, notes, and snippets.

@Tonel
Last active October 29, 2022 09:03
Show Gist options
  • Save Tonel/756a0c2968163dff990ff2b50ad4361f to your computer and use it in GitHub Desktop.
Save Tonel/756a0c2968163dff990ff2b50ad4361f to your computer and use it in GitHub Desktop.
// initializing the CriteriaBuilder and CriteriaQuery object
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Book> criteriaQuery = criteriaBuilder.createQuery(Book.class);
Root<Book> root = criteriaQuery.from(Book.class);
// defining the JOIN clauses
Join<Book, Author> author = root.join("authors");
Join<Book, Genre> genre = root.join("genres");
// specifying the WHERE conditions
criteriaQuery.where(
criteriaBuilder.equal(author.get("id"), 3),
criteriaBuilder.equal(genre.get("id"), 5)
);
// retrieving the list of books
List<Book> books = entityManager
.createQuery(criteriaQuery)
.getResultList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment