|
| 1 | +package com.baeldung.jpa.entitygraph.repo; |
| 2 | + |
| 3 | +import com.baeldung.jpa.entitygraph.model.Post; |
| 4 | + |
| 5 | +import javax.persistence.*; |
| 6 | +import javax.persistence.criteria.CriteriaBuilder; |
| 7 | +import javax.persistence.criteria.CriteriaQuery; |
| 8 | +import javax.persistence.criteria.Root; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +public class PostRepository { |
| 13 | + private EntityManagerFactory emf = null; |
| 14 | + |
| 15 | + |
| 16 | + public PostRepository() { |
| 17 | + Map properties = new HashMap(); |
| 18 | + properties.put("hibernate.show_sql", "true"); |
| 19 | + properties.put("hibernate.format_sql", "true"); |
| 20 | + emf = Persistence.createEntityManagerFactory("entity-graph-pu", properties); |
| 21 | + } |
| 22 | + |
| 23 | + public Post find(Long id) { |
| 24 | + EntityManager entityManager = emf.createEntityManager(); |
| 25 | + |
| 26 | + Post post = entityManager.find(Post.class, id); |
| 27 | + |
| 28 | + entityManager.close(); |
| 29 | + return post; |
| 30 | + } |
| 31 | + |
| 32 | + public Post findWithEntityGraph(Long id) { |
| 33 | + EntityManager entityManager = emf.createEntityManager(); |
| 34 | + |
| 35 | + EntityGraph entityGraph = entityManager.getEntityGraph("post-entity-graph"); |
| 36 | + Map<String, Object> properties = new HashMap<>(); |
| 37 | + properties.put("javax.persistence.fetchgraph", entityGraph); |
| 38 | + Post post = entityManager.find(Post.class, id, properties); |
| 39 | + |
| 40 | + entityManager.close(); |
| 41 | + return post; |
| 42 | + } |
| 43 | + |
| 44 | + public Post findWithEntityGraph2(Long id) { |
| 45 | + EntityManager entityManager = emf.createEntityManager(); |
| 46 | + |
| 47 | + EntityGraph<Post> entityGraph = entityManager.createEntityGraph(Post.class); |
| 48 | + entityGraph.addAttributeNodes("subject"); |
| 49 | + entityGraph.addAttributeNodes("user"); |
| 50 | + entityGraph.addSubgraph("comments") |
| 51 | + .addAttributeNodes("user"); |
| 52 | + |
| 53 | + Map<String, Object> properties = new HashMap<>(); |
| 54 | + properties.put("javax.persistence.fetchgraph", entityGraph); |
| 55 | + Post post = entityManager.find(Post.class, id, properties); |
| 56 | + |
| 57 | + entityManager.close(); |
| 58 | + return post; |
| 59 | + } |
| 60 | + |
| 61 | + public Post findUsingJpql(Long id) { |
| 62 | + EntityManager entityManager = emf.createEntityManager(); |
| 63 | + |
| 64 | + EntityGraph entityGraph = entityManager.getEntityGraph("post-entity-graph-with-comment-users"); |
| 65 | + Post post = entityManager.createQuery("Select p from Post p where p.id=:id", Post.class) |
| 66 | + .setParameter("id", id) |
| 67 | + .setHint("javax.persistence.fetchgraph", entityGraph) |
| 68 | + .getSingleResult(); |
| 69 | + |
| 70 | + entityManager.close(); |
| 71 | + return post; |
| 72 | + } |
| 73 | + |
| 74 | + public Post findUsingCriteria(Long id) { |
| 75 | + EntityManager entityManager = emf.createEntityManager(); |
| 76 | + |
| 77 | + EntityGraph entityGraph = entityManager.getEntityGraph("post-entity-graph-with-comment-users"); |
| 78 | + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); |
| 79 | + CriteriaQuery<Post> criteriaQuery = criteriaBuilder.createQuery(Post.class); |
| 80 | + Root<Post> root = criteriaQuery.from(Post.class); |
| 81 | + criteriaQuery.where(criteriaBuilder.equal(root.<Long>get("id"), id)); |
| 82 | + TypedQuery<Post> typedQuery = entityManager.createQuery(criteriaQuery); |
| 83 | + typedQuery.setHint("javax.persistence.loadgraph", entityGraph); |
| 84 | + Post post = typedQuery.getSingleResult(); |
| 85 | + |
| 86 | + entityManager.close(); |
| 87 | + return post; |
| 88 | + } |
| 89 | + |
| 90 | + public void clean() { |
| 91 | + emf.close(); |
| 92 | + } |
| 93 | +} |
0 commit comments