|
12 | 12 | import javax.persistence.EntityManager;
|
13 | 13 | import javax.persistence.PersistenceContext;
|
14 | 14 | import javax.persistence.PersistenceUnitUtil;
|
| 15 | +import java.util.Arrays; |
15 | 16 | import java.util.List;
|
16 | 17 |
|
17 |
| -import static org.junit.Assert.assertFalse; |
18 |
| -import static org.junit.Assert.assertTrue; |
| 18 | +import static org.junit.Assert.*; |
19 | 19 |
|
20 | 20 | /**
|
21 | 21 | * In this sample we're going to query a +JPA Entity+ and control property loading by providing +Hints+ using the new
|
@@ -144,4 +144,57 @@ public void testEntityGraphProgrammatically() throws Exception {
|
144 | 144 | assertTrue(persistenceUnitUtil.isLoaded(movie, "movieAwards"));
|
145 | 145 | }
|
146 | 146 | }
|
| 147 | + |
| 148 | + @Test |
| 149 | + public void testEntityGraphWithNamedParameters() throws Exception { |
| 150 | + PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil(); |
| 151 | + List<Movie> listMovieById = movieBean.listMoviesById(1, "javax.persistence.fetchgraph", "movieWithActors"); |
| 152 | + assertFalse(listMovieById.isEmpty()); |
| 153 | + assertEquals(1, listMovieById.size()); |
| 154 | + for (Movie movie : listMovieById) { |
| 155 | + assertTrue(movie.getId().equals(1)); |
| 156 | + |
| 157 | + assertTrue(persistenceUnitUtil.isLoaded(movie, "movieActors")); |
| 158 | + assertFalse(movie.getMovieActors().isEmpty()); |
| 159 | + for (MovieActor movieActor : movie.getMovieActors()) { |
| 160 | + assertFalse(persistenceUnitUtil.isLoaded(movieActor, "movieActorAwards")); |
| 161 | + } |
| 162 | + |
| 163 | + // https://hibernate.atlassian.net/browse/HHH-8776 |
| 164 | + // The specification states that by using fetchgraph, attributes should stay unloaded even if defined as |
| 165 | + // EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch |
| 166 | + // additional state. |
| 167 | + assertTrue(persistenceUnitUtil.isLoaded(movie, "movieDirectors") || |
| 168 | + !persistenceUnitUtil.isLoaded(movie, "movieDirectors")); |
| 169 | + assertFalse(persistenceUnitUtil.isLoaded(movie, "movieAwards")); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testEntityGraphWithNamedParametersList() throws Exception { |
| 175 | + PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil(); |
| 176 | + // Hibernate fails mixing Entity Graphs and Named Parameters with "IN". Throws NPE |
| 177 | + List<Movie> listMoviesByIds = |
| 178 | + movieBean.listMoviesByIds(Arrays.asList(1, 2), "javax.persistence.fetchgraph", "movieWithActors"); |
| 179 | + |
| 180 | + assertFalse(listMoviesByIds.isEmpty()); |
| 181 | + assertEquals(2, listMoviesByIds.size()); |
| 182 | + for (Movie movie : listMoviesByIds) { |
| 183 | + assertTrue(movie.getId().equals(1) || movie.getId().equals(2)); |
| 184 | + |
| 185 | + assertTrue(persistenceUnitUtil.isLoaded(movie, "movieActors")); |
| 186 | + assertFalse(movie.getMovieActors().isEmpty()); |
| 187 | + for (MovieActor movieActor : movie.getMovieActors()) { |
| 188 | + assertFalse(persistenceUnitUtil.isLoaded(movieActor, "movieActorAwards")); |
| 189 | + } |
| 190 | + |
| 191 | + // https://hibernate.atlassian.net/browse/HHH-8776 |
| 192 | + // The specification states that by using fetchgraph, attributes should stay unloaded even if defined as |
| 193 | + // EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch |
| 194 | + // additional state. |
| 195 | + assertTrue(persistenceUnitUtil.isLoaded(movie, "movieDirectors") || |
| 196 | + !persistenceUnitUtil.isLoaded(movie, "movieDirectors")); |
| 197 | + assertFalse(persistenceUnitUtil.isLoaded(movie, "movieAwards")); |
| 198 | + } |
| 199 | + } |
147 | 200 | }
|
0 commit comments