8
8
import org .junit .runner .RunWith ;
9
9
10
10
import javax .inject .Inject ;
11
+ import javax .persistence .EntityGraph ;
11
12
import javax .persistence .EntityManager ;
12
13
import javax .persistence .PersistenceContext ;
13
14
import javax .persistence .PersistenceUnitUtil ;
19
20
/**
20
21
* In this sample we're going to query a +JPA Entity+ and control property loading by providing +Hints+ using the new
21
22
* +JPA Entity Graph+ API.
23
+ * <p/>
24
+ * Entity Graphs are used in the specification of fetch plans for query or find operations.
22
25
*
23
26
* @author Roberto Cortez
24
27
*/
@@ -44,7 +47,7 @@ public static WebArchive createDeployment() {
44
47
@ Test
45
48
public void testEntityGraphMovieDefault () throws Exception {
46
49
PersistenceUnitUtil persistenceUnitUtil = entityManager .getEntityManagerFactory ().getPersistenceUnitUtil ();
47
- List <Movie > listMoviesDefaultFetch = movieBean .listMoviesDefault ();
50
+ List <Movie > listMoviesDefaultFetch = movieBean .listMovies ();
48
51
for (Movie movie : listMoviesDefaultFetch ) {
49
52
assertFalse (persistenceUnitUtil .isLoaded (movie , "movieActors" ));
50
53
assertTrue (persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
@@ -55,7 +58,7 @@ public void testEntityGraphMovieDefault() throws Exception {
55
58
@ Test
56
59
public void testEntityGraphMovieWithActors () throws Exception {
57
60
PersistenceUnitUtil persistenceUnitUtil = entityManager .getEntityManagerFactory ().getPersistenceUnitUtil ();
58
- List <Movie > listMoviesWithActorsFetch = movieBean .listMoviesWithActorsFetch ( );
61
+ List <Movie > listMoviesWithActorsFetch = movieBean .listMovies ( "javax.persistence.fetchgraph" , "movieWithActors" );
59
62
for (Movie movie : listMoviesWithActorsFetch ) {
60
63
assertTrue (persistenceUnitUtil .isLoaded (movie , "movieActors" ));
61
64
assertFalse (movie .getMovieActors ().isEmpty ());
@@ -68,11 +71,11 @@ public void testEntityGraphMovieWithActors() throws Exception {
68
71
// EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch
69
72
// additional state.
70
73
assertTrue (persistenceUnitUtil .isLoaded (movie , "movieDirectors" ) ||
71
- !persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
74
+ !persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
72
75
assertFalse (persistenceUnitUtil .isLoaded (movie , "movieAwards" ));
73
76
}
74
77
75
- List <Movie > listMoviesWithActorsLoad = movieBean .listMoviesWithActorsLoad ( );
78
+ List <Movie > listMoviesWithActorsLoad = movieBean .listMovies ( "javax.persistence.loadgraph" , "movieWithActors" );
76
79
for (Movie movie : listMoviesWithActorsLoad ) {
77
80
// https://java.net/jira/browse/GLASSFISH-21200
78
81
// Glassfish is not processing "javax.persistence.loadgraph".
@@ -90,25 +93,27 @@ public void testEntityGraphMovieWithActors() throws Exception {
90
93
@ Test
91
94
public void testEntityGraphMovieWithActorsAndAwards () throws Exception {
92
95
PersistenceUnitUtil persistenceUnitUtil = entityManager .getEntityManagerFactory ().getPersistenceUnitUtil ();
93
- List <Movie > listMoviesWithActorsFetch = movieBean .listMoviesWithActorsAndAwardsFetch ();
96
+ List <Movie > listMoviesWithActorsFetch =
97
+ movieBean .listMovies ("javax.persistence.fetchgraph" , "movieWithActorsAndAwards" );
94
98
for (Movie movie : listMoviesWithActorsFetch ) {
95
99
assertTrue (persistenceUnitUtil .isLoaded (movie , "movieActors" ));
96
100
assertFalse (movie .getMovieActors ().isEmpty ());
97
101
for (MovieActor movieActor : movie .getMovieActors ()) {
98
102
assertTrue (persistenceUnitUtil .isLoaded (movieActor , "movieActorAwards" ) ||
99
- !persistenceUnitUtil .isLoaded (movieActor , "movieActorAwards" ));
103
+ !persistenceUnitUtil .isLoaded (movieActor , "movieActorAwards" ));
100
104
}
101
105
102
106
// https://hibernate.atlassian.net/browse/HHH-8776
103
107
// The specification states that by using fetchgraph, attributes should stay unloaded even if defined as
104
108
// EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch
105
109
// additional state.
106
110
assertTrue (persistenceUnitUtil .isLoaded (movie , "movieDirectors" ) ||
107
- !persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
111
+ !persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
108
112
assertFalse (persistenceUnitUtil .isLoaded (movie , "movieAwards" ));
109
113
}
110
114
111
- List <Movie > listMoviesWithActorsLoad = movieBean .listMoviesWithActorsAndAwardsLoad ();
115
+ List <Movie > listMoviesWithActorsLoad =
116
+ movieBean .listMovies ("javax.persistence.loadgraph" , "movieWithActorsAndAwards" );
112
117
for (Movie movie : listMoviesWithActorsLoad ) {
113
118
// https://java.net/jira/browse/GLASSFISH-21200
114
119
// Glassfish is not processing "javax.persistence.loadgraph".
@@ -122,4 +127,21 @@ public void testEntityGraphMovieWithActorsAndAwards() throws Exception {
122
127
assertFalse (persistenceUnitUtil .isLoaded (movie , "movieAwards" ));
123
128
}
124
129
}
130
+
131
+ @ Test
132
+ public void testEntityGraphProgrammatically () throws Exception {
133
+ PersistenceUnitUtil persistenceUnitUtil = entityManager .getEntityManagerFactory ().getPersistenceUnitUtil ();
134
+
135
+ EntityGraph <Movie > fetchAll = entityManager .createEntityGraph (Movie .class );
136
+ fetchAll .addSubgraph (Movie_ .movieActors );
137
+ fetchAll .addSubgraph (Movie_ .movieDirectors );
138
+ fetchAll .addSubgraph (Movie_ .movieAwards );
139
+
140
+ List <Movie > moviesFetchAll = movieBean .listMovies ("javax.persistence.fetchgraph" , fetchAll );
141
+ for (Movie movie : moviesFetchAll ) {
142
+ assertTrue (persistenceUnitUtil .isLoaded (movie , "movieActors" ));
143
+ assertTrue (persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
144
+ assertTrue (persistenceUnitUtil .isLoaded (movie , "movieAwards" ));
145
+ }
146
+ }
125
147
}
0 commit comments