Skip to content

Commit eaeed72

Browse files
authored
Merge pull request eugenp#5764 from eelhazati/master
BAEL-2395: JPA Entity Graph.
2 parents a6c9c58 + 8d39316 commit eaeed72

8 files changed

Lines changed: 428 additions & 19 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.jpa.entitygraph;
2+
3+
import com.baeldung.jpa.entitygraph.model.Post;
4+
import com.baeldung.jpa.entitygraph.repo.PostRepository;
5+
6+
public class MainApp {
7+
8+
public static void main(String... args) {
9+
Long postId = 1L;
10+
Post post = null;
11+
PostRepository postRepository = new PostRepository();
12+
13+
//Using EntityManager.find().
14+
post = postRepository.find(postId);
15+
post = postRepository.findWithEntityGraph(postId);
16+
post = postRepository.findWithEntityGraph2(postId);
17+
18+
//Using JPQL: Query and TypedQuery
19+
post = postRepository.findUsingJpql(postId);
20+
21+
//Using Criteria API
22+
post = postRepository.findUsingCriteria(postId);
23+
24+
postRepository.clean();
25+
}
26+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.jpa.entitygraph.model;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
public class Comment {
7+
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private Long id;
11+
12+
private String reply;
13+
14+
@ManyToOne(fetch = FetchType.LAZY)
15+
@JoinColumn
16+
private Post post;
17+
18+
@ManyToOne(fetch = FetchType.LAZY)
19+
@JoinColumn
20+
private User user;
21+
//...
22+
23+
public Comment() {
24+
}
25+
26+
public Comment(String reply, Post post, User user) {
27+
this.reply = reply;
28+
this.post = post;
29+
this.user = user;
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getReply() {
41+
return reply;
42+
}
43+
44+
public void setReply(String reply) {
45+
this.reply = reply;
46+
}
47+
48+
public Post getPost() {
49+
return post;
50+
}
51+
52+
public void setPost(Post post) {
53+
this.post = post;
54+
}
55+
56+
public User getUser() {
57+
return user;
58+
}
59+
60+
public void setUser(User user) {
61+
this.user = user;
62+
}
63+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.jpa.entitygraph.model;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
@NamedEntityGraph(
9+
name = "post-entity-graph",
10+
attributeNodes = {
11+
@NamedAttributeNode("subject"),
12+
@NamedAttributeNode("user"),
13+
@NamedAttributeNode("comments"),
14+
}
15+
)
16+
@NamedEntityGraph(
17+
name = "post-entity-graph-with-comment-users",
18+
attributeNodes = {
19+
@NamedAttributeNode("subject"),
20+
@NamedAttributeNode("user"),
21+
@NamedAttributeNode(value = "comments", subgraph = "comments-subgraph"),
22+
},
23+
subgraphs = {
24+
@NamedSubgraph(
25+
name = "comments-subgraph",
26+
attributeNodes = {
27+
@NamedAttributeNode("user")
28+
}
29+
)
30+
}
31+
)
32+
@Entity
33+
public class Post {
34+
35+
@Id
36+
@GeneratedValue(strategy = GenerationType.IDENTITY)
37+
private Long id;
38+
private String subject;
39+
@OneToMany(mappedBy = "post")
40+
private List<Comment> comments = new ArrayList<>();
41+
42+
@ManyToOne(fetch = FetchType.LAZY)
43+
@JoinColumn
44+
private User user;
45+
//...
46+
47+
public Post() {
48+
}
49+
50+
public Post(String subject, User user) {
51+
this.subject = subject;
52+
this.user = user;
53+
}
54+
55+
public Long getId() {
56+
return id;
57+
}
58+
59+
public void setId(Long id) {
60+
this.id = id;
61+
}
62+
63+
public String getSubject() {
64+
return subject;
65+
}
66+
67+
public void setSubject(String subject) {
68+
this.subject = subject;
69+
}
70+
71+
public List<Comment> getComments() {
72+
return comments;
73+
}
74+
75+
public void setComments(List<Comment> comments) {
76+
this.comments = comments;
77+
}
78+
79+
public User getUser() {
80+
return user;
81+
}
82+
83+
public void setUser(User user) {
84+
this.user = user;
85+
}
86+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.jpa.entitygraph.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class User {
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY)
12+
private Long id;
13+
private String name;
14+
private String email;
15+
16+
//...
17+
18+
public User() {
19+
}
20+
21+
public User(String email) {
22+
this.email = email;
23+
}
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public String getEmail() {
42+
return email;
43+
}
44+
45+
public void setEmail(String email) {
46+
this.email = email;
47+
}
48+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)