Last active
October 29, 2022 09:03
-
-
Save Tonel/756a0c2968163dff990ff2b50ad4361f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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