Skip to content

Commit 1fa8220

Browse files
committed
Add type-safe Entity Graphs
1 parent 4d34c5d commit 1fa8220

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

core/src/test/java/com/vladmihalcea/hpjp/hibernate/fetching/EntityGraphTest.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.vladmihalcea.hpjp.hibernate.fetching;
22

33
import com.vladmihalcea.hpjp.util.AbstractPostgreSQLIntegrationTest;
4+
import jakarta.persistence.*;
5+
import org.hibernate.jpa.SpecHints;
46
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
57
import org.junit.jupiter.api.Test;
6-
import org.junit.jupiter.api.extension.ExtendWith;
78

8-
import jakarta.persistence.*;
99
import java.util.ArrayList;
1010
import java.util.Collections;
1111
import java.util.Date;
1212
import java.util.List;
1313

14+
import static com.vladmihalcea.hpjp.hibernate.fetching.EntityGraphTest_.*;
1415
import static org.junit.jupiter.api.Assertions.*;
15-
import org.junit.runner.RunWith;
1616

1717
/**
1818
* @author Vlad Mihalcea
@@ -76,8 +76,8 @@ public void testFindWithNamedEntityFetchGraph() {
7676

7777
return entityManager.find(PostComment.class, 1L,
7878
Collections.singletonMap(
79-
"jakarta.persistence.fetchgraph",
80-
entityManager.getEntityGraph("PostComment.post")
79+
SpecHints.HINT_SPEC_FETCH_GRAPH,
80+
entityManager.getEntityGraph(PostComment_.GRAPH_POST_COMMENT_POST)
8181
)
8282
);
8383
});
@@ -89,13 +89,13 @@ public void testFindWithNamedEntityFetchGraph() {
8989
public void testFindUsingNestedEntityGraph() {
9090
PostCommentDetails commentDetails = doInJPA(entityManager -> {
9191
EntityGraph<PostCommentDetails> commentDetailsGraph = entityManager.createEntityGraph(PostCommentDetails.class);
92-
commentDetailsGraph.addAttributeNodes("comment");
93-
Subgraph<PostComment> commentSubgraph = commentDetailsGraph.addSubgraph("comment");
94-
commentSubgraph.addAttributeNodes("post");
92+
commentDetailsGraph.addAttributeNodes(PostCommentDetails_.COMMENT);
93+
Subgraph<PostComment> commentSubgraph = commentDetailsGraph.addSubgraph(PostCommentDetails_.COMMENT);
94+
commentSubgraph.addAttributeNodes(PostComment_.POST);
9595

9696
return entityManager.find(PostCommentDetails.class, 1L,
9797
Collections.singletonMap(
98-
"jakarta.persistence.loadgraph",
98+
SpecHints.HINT_SPEC_LOAD_GRAPH,
9999
commentDetailsGraph
100100
)
101101
);
@@ -110,8 +110,8 @@ public void testFindWithNamedEntityLoadGraph() {
110110
PostComment comment = doInJPA(entityManager -> {
111111
return entityManager.find(PostComment.class, 1L,
112112
Collections.singletonMap(
113-
"jakarta.persistence.loadgraph",
114-
entityManager.getEntityGraph("PostComment.post")
113+
SpecHints.HINT_SPEC_LOAD_GRAPH,
114+
entityManager.getEntityGraph(PostComment_.GRAPH_POST_COMMENT_POST)
115115
)
116116
);
117117
});
@@ -123,8 +123,8 @@ public void testFindPostWithAllAssociations() {
123123
Post post = doInJPA(entityManager -> {
124124
return entityManager.find(Post.class, 1L,
125125
Collections.singletonMap(
126-
"jakarta.persistence.fetchgraph",
127-
entityManager.getEntityGraph("Post.all")
126+
SpecHints.HINT_SPEC_FETCH_GRAPH,
127+
entityManager.getEntityGraph(Post_.GRAPH_POST_ALL)
128128
)
129129
);
130130
});
@@ -138,18 +138,21 @@ public void testFindPostWithAllAssociations() {
138138
@Entity(name = "Post")
139139
@Table(name = "post")
140140
@NamedEntityGraph(
141-
name = "Post.details",
142-
attributeNodes = @NamedAttributeNode("details")
141+
name = Post_.GRAPH_POST_DETAILS,
142+
attributeNodes = @NamedAttributeNode(Post_.DETAILS)
143143
)
144144
@NamedEntityGraph(
145-
name = "Post.all",
145+
name = Post_.GRAPH_POST_ALL,
146146
attributeNodes = {
147-
@NamedAttributeNode("details"),
148-
@NamedAttributeNode(value = "comments", subgraph = "Post.comment.details"),
147+
@NamedAttributeNode(Post_.DETAILS),
148+
@NamedAttributeNode(
149+
value = Post_.COMMENTS,
150+
subgraph = PostComment_.DETAILS
151+
),
149152
},
150153
subgraphs = @NamedSubgraph(
151-
name = "Post.comment.details",
152-
attributeNodes = @NamedAttributeNode("details")
154+
name = PostComment_.DETAILS,
155+
attributeNodes = @NamedAttributeNode(PostComment_.DETAILS)
153156
)
154157
)
155158
public static class Post {
@@ -277,8 +280,8 @@ public PostDetails setCreatedBy(String createdBy) {
277280
@Entity(name = "PostComment")
278281
@Table(name = "post_comment")
279282
@NamedEntityGraph(
280-
name = "PostComment.post",
281-
attributeNodes = @NamedAttributeNode("post")
283+
name = PostComment_.GRAPH_POST_COMMENT_POST,
284+
attributeNodes = @NamedAttributeNode(PostComment_.POST)
282285
)
283286
public static class PostComment {
284287

0 commit comments

Comments
 (0)