Last active
September 15, 2015 18:26
-
-
Save thjanssen/0f641e5a56513604be73 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
CriteriaBuilder cb = this.em.getCriteriaBuilder(); | |
// create the query | |
CriteriaQuery<Author> q = cb.createQuery(Author.class); | |
// set the root class | |
Root<Author> a = q.from(Author.class); | |
// use metadata class to define the where clause | |
q.where(cb.like(a.get(Author_.firstName), "J%")); | |
// perform query | |
this.em.createQuery(q).getResultList(); |
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
// create the entity graph | |
EntityGraph graph = this.em.createEntityGraph(Author.class); | |
// use metadata class to define the subgraph | |
Subgraph<Book> bookSubGraph = graph.addSubgraph(Author_.books); | |
// perform query | |
List<Author> authors = this.em | |
.createQuery("SELECT DISTINCT a FROM Author a", Author.class) | |
.setHint("javax.persistence.fetchgraph", graph).getResultList(); |
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
... | |
<dependencies> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-jpamodelgen</artifactId> | |
</dependency> | |
</dependencies> | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment