88import org .junit .runner .RunWith ;
99
1010import javax .inject .Inject ;
11+ import javax .persistence .EntityGraph ;
1112import javax .persistence .EntityManager ;
1213import javax .persistence .PersistenceContext ;
1314import javax .persistence .PersistenceUnitUtil ;
1920/**
2021 * In this sample we're going to query a +JPA Entity+ and control property loading by providing +Hints+ using the new
2122 * +JPA Entity Graph+ API.
23+ * <p/>
24+ * Entity Graphs are used in the specification of fetch plans for query or find operations.
2225 *
2326 * @author Roberto Cortez
2427 */
@@ -44,7 +47,7 @@ public static WebArchive createDeployment() {
4447 @ Test
4548 public void testEntityGraphMovieDefault () throws Exception {
4649 PersistenceUnitUtil persistenceUnitUtil = entityManager .getEntityManagerFactory ().getPersistenceUnitUtil ();
47- List <Movie > listMoviesDefaultFetch = movieBean .listMoviesDefault ();
50+ List <Movie > listMoviesDefaultFetch = movieBean .listMovies ();
4851 for (Movie movie : listMoviesDefaultFetch ) {
4952 assertFalse (persistenceUnitUtil .isLoaded (movie , "movieActors" ));
5053 assertTrue (persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
@@ -55,7 +58,7 @@ public void testEntityGraphMovieDefault() throws Exception {
5558 @ Test
5659 public void testEntityGraphMovieWithActors () throws Exception {
5760 PersistenceUnitUtil persistenceUnitUtil = entityManager .getEntityManagerFactory ().getPersistenceUnitUtil ();
58- List <Movie > listMoviesWithActorsFetch = movieBean .listMoviesWithActorsFetch ( );
61+ List <Movie > listMoviesWithActorsFetch = movieBean .listMovies ( "javax.persistence.fetchgraph" , "movieWithActors" );
5962 for (Movie movie : listMoviesWithActorsFetch ) {
6063 assertTrue (persistenceUnitUtil .isLoaded (movie , "movieActors" ));
6164 assertFalse (movie .getMovieActors ().isEmpty ());
@@ -68,11 +71,11 @@ public void testEntityGraphMovieWithActors() throws Exception {
6871 // EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch
6972 // additional state.
7073 assertTrue (persistenceUnitUtil .isLoaded (movie , "movieDirectors" ) ||
71- !persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
74+ !persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
7275 assertFalse (persistenceUnitUtil .isLoaded (movie , "movieAwards" ));
7376 }
7477
75- List <Movie > listMoviesWithActorsLoad = movieBean .listMoviesWithActorsLoad ( );
78+ List <Movie > listMoviesWithActorsLoad = movieBean .listMovies ( "javax.persistence.loadgraph" , "movieWithActors" );
7679 for (Movie movie : listMoviesWithActorsLoad ) {
7780 // https://java.net/jira/browse/GLASSFISH-21200
7881 // Glassfish is not processing "javax.persistence.loadgraph".
@@ -90,25 +93,27 @@ public void testEntityGraphMovieWithActors() throws Exception {
9093 @ Test
9194 public void testEntityGraphMovieWithActorsAndAwards () throws Exception {
9295 PersistenceUnitUtil persistenceUnitUtil = entityManager .getEntityManagerFactory ().getPersistenceUnitUtil ();
93- List <Movie > listMoviesWithActorsFetch = movieBean .listMoviesWithActorsAndAwardsFetch ();
96+ List <Movie > listMoviesWithActorsFetch =
97+ movieBean .listMovies ("javax.persistence.fetchgraph" , "movieWithActorsAndAwards" );
9498 for (Movie movie : listMoviesWithActorsFetch ) {
9599 assertTrue (persistenceUnitUtil .isLoaded (movie , "movieActors" ));
96100 assertFalse (movie .getMovieActors ().isEmpty ());
97101 for (MovieActor movieActor : movie .getMovieActors ()) {
98102 assertTrue (persistenceUnitUtil .isLoaded (movieActor , "movieActorAwards" ) ||
99- !persistenceUnitUtil .isLoaded (movieActor , "movieActorAwards" ));
103+ !persistenceUnitUtil .isLoaded (movieActor , "movieActorAwards" ));
100104 }
101105
102106 // https://hibernate.atlassian.net/browse/HHH-8776
103107 // The specification states that by using fetchgraph, attributes should stay unloaded even if defined as
104108 // EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch
105109 // additional state.
106110 assertTrue (persistenceUnitUtil .isLoaded (movie , "movieDirectors" ) ||
107- !persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
111+ !persistenceUnitUtil .isLoaded (movie , "movieDirectors" ));
108112 assertFalse (persistenceUnitUtil .isLoaded (movie , "movieAwards" ));
109113 }
110114
111- List <Movie > listMoviesWithActorsLoad = movieBean .listMoviesWithActorsAndAwardsLoad ();
115+ List <Movie > listMoviesWithActorsLoad =
116+ movieBean .listMovies ("javax.persistence.loadgraph" , "movieWithActorsAndAwards" );
112117 for (Movie movie : listMoviesWithActorsLoad ) {
113118 // https://java.net/jira/browse/GLASSFISH-21200
114119 // Glassfish is not processing "javax.persistence.loadgraph".
@@ -122,4 +127,21 @@ public void testEntityGraphMovieWithActorsAndAwards() throws Exception {
122127 assertFalse (persistenceUnitUtil .isLoaded (movie , "movieAwards" ));
123128 }
124129 }
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+ }
125147}
0 commit comments