JPA Entity Graph.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.jpa.entitygraph;
|
||||
|
||||
import com.baeldung.jpa.entitygraph.model.Post;
|
||||
import com.baeldung.jpa.entitygraph.repo.PostRepository;
|
||||
|
||||
public class MainApp {
|
||||
|
||||
public static void main(String... args) {
|
||||
Long postId = 1L;
|
||||
Post post = null;
|
||||
PostRepository postRepository = new PostRepository();
|
||||
|
||||
//Using EntityManager.find().
|
||||
post = postRepository.find(postId);
|
||||
post = postRepository.findWithEntityGraph(postId);
|
||||
post = postRepository.findWithEntityGraph2(postId);
|
||||
|
||||
//Using JPQL: Query and TypedQuery
|
||||
post = postRepository.findUsingJpql(postId);
|
||||
|
||||
//Using Criteria API
|
||||
post = postRepository.findUsingCriteria(postId);
|
||||
|
||||
postRepository.clean();
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.jpa.entitygraph.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Comment {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String reply;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
private Post post;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
private User user;
|
||||
//...
|
||||
|
||||
public Comment() {
|
||||
}
|
||||
|
||||
public Comment(String reply, Post post, User user) {
|
||||
this.reply = reply;
|
||||
this.post = post;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getReply() {
|
||||
return reply;
|
||||
}
|
||||
|
||||
public void setReply(String reply) {
|
||||
this.reply = reply;
|
||||
}
|
||||
|
||||
public Post getPost() {
|
||||
return post;
|
||||
}
|
||||
|
||||
public void setPost(Post post) {
|
||||
this.post = post;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.jpa.entitygraph.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@NamedEntityGraph(
|
||||
name = "post-entity-graph",
|
||||
attributeNodes = {
|
||||
@NamedAttributeNode("subject"),
|
||||
@NamedAttributeNode("user"),
|
||||
@NamedAttributeNode("comments"),
|
||||
}
|
||||
)
|
||||
@NamedEntityGraph(
|
||||
name = "post-entity-graph-with-comment-users",
|
||||
attributeNodes = {
|
||||
@NamedAttributeNode("subject"),
|
||||
@NamedAttributeNode("user"),
|
||||
@NamedAttributeNode(value = "comments", subgraph = "comments-subgraph"),
|
||||
},
|
||||
subgraphs = {
|
||||
@NamedSubgraph(
|
||||
name = "comments-subgraph",
|
||||
attributeNodes = {
|
||||
@NamedAttributeNode("user")
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
@Entity
|
||||
public class Post {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String subject;
|
||||
@OneToMany(mappedBy = "post")
|
||||
private List<Comment> comments = new ArrayList<>();
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
private User user;
|
||||
//...
|
||||
|
||||
public Post() {
|
||||
}
|
||||
|
||||
public Post(String subject, User user) {
|
||||
this.subject = subject;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
public List<Comment> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public void setComments(List<Comment> comments) {
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.jpa.entitygraph.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
//...
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.jpa.entitygraph.repo;
|
||||
|
||||
import com.baeldung.jpa.entitygraph.model.Post;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PostRepository {
|
||||
private EntityManagerFactory emf = null;
|
||||
|
||||
|
||||
public PostRepository() {
|
||||
Map properties = new HashMap();
|
||||
properties.put("hibernate.show_sql", "true");
|
||||
properties.put("hibernate.format_sql", "true");
|
||||
emf = Persistence.createEntityManagerFactory("entity-graph-pu", properties);
|
||||
}
|
||||
|
||||
public Post find(Long id) {
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
|
||||
Post post = entityManager.find(Post.class, id);
|
||||
|
||||
entityManager.close();
|
||||
return post;
|
||||
}
|
||||
|
||||
public Post findWithEntityGraph(Long id) {
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
|
||||
EntityGraph entityGraph = entityManager.getEntityGraph("post-entity-graph");
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put("javax.persistence.fetchgraph", entityGraph);
|
||||
Post post = entityManager.find(Post.class, id, properties);
|
||||
|
||||
entityManager.close();
|
||||
return post;
|
||||
}
|
||||
|
||||
public Post findWithEntityGraph2(Long id) {
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
|
||||
EntityGraph<Post> entityGraph = entityManager.createEntityGraph(Post.class);
|
||||
entityGraph.addAttributeNodes("subject");
|
||||
entityGraph.addAttributeNodes("user");
|
||||
entityGraph.addSubgraph("comments")
|
||||
.addAttributeNodes("user");
|
||||
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put("javax.persistence.fetchgraph", entityGraph);
|
||||
Post post = entityManager.find(Post.class, id, properties);
|
||||
|
||||
entityManager.close();
|
||||
return post;
|
||||
}
|
||||
|
||||
public Post findUsingJpql(Long id) {
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
|
||||
EntityGraph entityGraph = entityManager.getEntityGraph("post-entity-graph-with-comment-users");
|
||||
Post post = entityManager.createQuery("Select p from Post p where p.id=:id", Post.class)
|
||||
.setParameter("id", id)
|
||||
.setHint("javax.persistence.fetchgraph", entityGraph)
|
||||
.getSingleResult();
|
||||
|
||||
entityManager.close();
|
||||
return post;
|
||||
}
|
||||
|
||||
public Post findUsingCriteria(Long id) {
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
|
||||
EntityGraph entityGraph = entityManager.getEntityGraph("post-entity-graph-with-comment-users");
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Post> criteriaQuery = criteriaBuilder.createQuery(Post.class);
|
||||
Root<Post> root = criteriaQuery.from(Post.class);
|
||||
criteriaQuery.where(criteriaBuilder.equal(root.<Long>get("id"), id));
|
||||
TypedQuery<Post> typedQuery = entityManager.createQuery(criteriaQuery);
|
||||
typedQuery.setHint("javax.persistence.loadgraph", entityGraph);
|
||||
Post post = typedQuery.getSingleResult();
|
||||
|
||||
entityManager.close();
|
||||
return post;
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
emf.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user