Skip to content

Commit 00314e3

Browse files
committed
DATAJPA-1041 - Polishing.
Ignored test cases failing on EclipseLink. Formatting and a bit of Javadoc. EntityGraphRepositoryMethodsIntegrationTests.shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() has to be ignored now as the new wiping of the EntityManager - which is needed to actually make the test test what's intended to be tested - reveals the same Eclipselink issue the newly introduced tests reveal, too.
1 parent af8631d commit 00314e3

File tree

5 files changed

+91
-99
lines changed

5 files changed

+91
-99
lines changed

src/main/java/org/springframework/data/jpa/repository/query/Jpa21Utils.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ static void configureFetchGraphFrom(JpaEntityGraph jpaEntityGraph, EntityGraph<?
157157
// Fast path - just single attribute
158158
if (!path.contains(".")) {
159159

160-
if(findAttributeNode(path, entityGraph) == null) {
160+
if (findAttributeNode(path, entityGraph) == null) {
161161
entityGraph.addAttributeNodes(path);
162162
}
163+
163164
continue;
164165
}
165166

@@ -175,26 +176,50 @@ static void configureFetchGraphFrom(JpaEntityGraph jpaEntityGraph, EntityGraph<?
175176
}
176177
}
177178

178-
private static Subgraph<?> findOrCreateSubgraph(String attributeNode, EntityGraph<?> entityGraph) {
179+
/**
180+
* Returns the {@link Subgraph} with the given name fro the given {@link EntityGraph} or creates a new one if none
181+
* already available.
182+
*
183+
* @param name
184+
* @param entityGraph
185+
* @return
186+
*/
187+
private static Subgraph<?> findOrCreateSubgraph(String name, EntityGraph<?> entityGraph) {
188+
189+
Subgraph<?> subgraph = findSubgraph(name, entityGraph);
179190

180-
Subgraph<?> subgraph = findSubgraph(attributeNode, entityGraph);
181-
return subgraph != null ? subgraph : entityGraph.addSubgraph(attributeNode);
191+
return subgraph != null ? subgraph : entityGraph.addSubgraph(name);
182192
}
183193

184-
private static Subgraph<?> findSubgraph(String attributeNode, EntityGraph<?> entityGraph) {
194+
/**
195+
* Returns the {@link Subgraph} with the given name from the given {@link EntityGraph}.
196+
*
197+
* @param name
198+
* @param entityGraph
199+
* @return
200+
*/
201+
private static Subgraph<?> findSubgraph(String name, EntityGraph<?> entityGraph) {
202+
203+
AttributeNode<?> node = findAttributeNode(name, entityGraph);
185204

186-
AttributeNode<?> node = findAttributeNode(attributeNode, entityGraph);
187-
if(node != null && !ObjectUtils.isEmpty(node.getSubgraphs())) {
205+
if (node != null && !ObjectUtils.isEmpty(node.getSubgraphs())) {
188206
return node.getSubgraphs().values().iterator().next();
189207
}
190208

191209
return null;
192210
}
193211

194-
private static AttributeNode<?> findAttributeNode(String attributeNode, EntityGraph<?> entityGraph) {
212+
/**
213+
* Returns the {@link AttributeNode} with the given name if present in the given {@link EntityGraph}.
214+
*
215+
* @param name
216+
* @param entityGraph must not be {@literal null}.
217+
* @return
218+
*/
219+
private static AttributeNode<?> findAttributeNode(String name, EntityGraph<?> entityGraph) {
195220

196-
for(AttributeNode<?> node : entityGraph.getAttributeNodes()) {
197-
if(ObjectUtils.nullSafeEquals(node.getAttributeName(), attributeNode)) {
221+
for (AttributeNode<?> node : entityGraph.getAttributeNodes()) {
222+
if (ObjectUtils.nullSafeEquals(node.getAttributeName(), name)) {
198223
return node;
199224
}
200225
}

src/test/java/org/springframework/data/jpa/domain/sample/User.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,27 @@
5353
* @author Christoph Strobl
5454
*/
5555
@Entity
56-
@NamedEntityGraphs({
57-
@NamedEntityGraph(name = "User.overview", attributeNodes = { @NamedAttributeNode("roles") }),
58-
@NamedEntityGraph(name = "User.detail", attributeNodes = { @NamedAttributeNode("roles"),
59-
@NamedAttributeNode("manager"), @NamedAttributeNode("colleagues") }),
60-
@NamedEntityGraph(name = "User.getOneWithDefinedEntityGraphById", attributeNodes = { @NamedAttributeNode("roles"),
61-
@NamedAttributeNode("manager"), @NamedAttributeNode("colleagues") }),
62-
@NamedEntityGraph(name = "User.withSubGraph",
63-
attributeNodes = {
64-
@NamedAttributeNode("roles"),
65-
@NamedAttributeNode(value="colleagues", subgraph = "User.colleagues")
66-
},
67-
subgraphs = {
68-
@NamedSubgraph(name = "User.colleagues", attributeNodes = {@NamedAttributeNode("colleagues"), @NamedAttributeNode("roles")})
69-
}
70-
)})
56+
@NamedEntityGraphs({ @NamedEntityGraph(name = "User.overview", attributeNodes = { @NamedAttributeNode("roles") }),
57+
@NamedEntityGraph(name = "User.detail",
58+
attributeNodes = { @NamedAttributeNode("roles"), @NamedAttributeNode("manager"),
59+
@NamedAttributeNode("colleagues") }),
60+
@NamedEntityGraph(name = "User.getOneWithDefinedEntityGraphById",
61+
attributeNodes = { @NamedAttributeNode("roles"), @NamedAttributeNode("manager"),
62+
@NamedAttributeNode("colleagues") }),
63+
@NamedEntityGraph(name = "User.withSubGraph",
64+
attributeNodes = { @NamedAttributeNode("roles"),
65+
@NamedAttributeNode(value = "colleagues", subgraph = "User.colleagues") },
66+
subgraphs = { @NamedSubgraph(name = "User.colleagues",
67+
attributeNodes = { @NamedAttributeNode("colleagues"), @NamedAttributeNode("roles") }) }) })
7168
@NamedQuery(name = "User.findByEmailAddress", query = "SELECT u FROM User u WHERE u.emailAddress = ?1")
7269
@NamedStoredProcedureQueries({ //
73-
@NamedStoredProcedureQuery(name = "User.plus1", procedureName = "plus1inout", parameters = {
74-
@StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
75-
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class) }) //
70+
@NamedStoredProcedureQuery(name = "User.plus1", procedureName = "plus1inout",
71+
parameters = { @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
72+
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class) }) //
7673
})
77-
@NamedStoredProcedureQuery(name = "User.plus1IO", procedureName = "plus1inout", parameters = {
78-
@StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
79-
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class) })
74+
@NamedStoredProcedureQuery(name = "User.plus1IO", procedureName = "plus1inout",
75+
parameters = { @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
76+
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class) })
8077
@Table(name = "SD_User")
8178
public class User {
8279

@@ -392,7 +389,7 @@ public Date getDateOfBirth() {
392389
public void setDateOfBirth(Date dateOfBirth) {
393390
this.dateOfBirth = dateOfBirth;
394391
}
395-
392+
396393
public void setCreatedAt(Date createdAt) {
397394
this.createdAt = createdAt;
398395
}

src/test/java/org/springframework/data/jpa/repository/EclipseLinkEntityGraphRepositoryMethodsIntegrationTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,26 @@
1515
*/
1616
package org.springframework.data.jpa.repository;
1717

18+
import org.junit.Ignore;
19+
import org.junit.Test;
1820
import org.springframework.test.context.ContextConfiguration;
1921

2022
/**
2123
* @author Oliver Gierke
2224
*/
2325
@ContextConfiguration("classpath:eclipselink.xml")
24-
public class EclipseLinkEntityGraphRepositoryMethodsIntegrationTests extends
25-
EntityGraphRepositoryMethodsIntegrationTests {
26+
public class EclipseLinkEntityGraphRepositoryMethodsIntegrationTests
27+
extends EntityGraphRepositoryMethodsIntegrationTests {
2628

29+
@Ignore
30+
@Test
31+
public void shouldRespectNamedEntitySubGraph() {}
32+
33+
@Ignore
34+
@Test
35+
public void shouldRespectMultipleSubGraphForSameAttributeWithDynamicFetchGraph() {}
36+
37+
@Ignore
38+
@Test
39+
public void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {}
2740
}

src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import static org.junit.Assert.*;
2020
import static org.springframework.data.jpa.support.EntityManagerTestUtils.*;
2121

22-
import java.util.Iterator;
2322
import java.util.List;
24-
import java.util.Map;
2523

26-
import javax.persistence.*;
24+
import javax.persistence.EntityManager;
25+
import javax.persistence.Persistence;
26+
import javax.persistence.PersistenceUtil;
2727

2828
import org.junit.Assume;
2929
import org.junit.Before;
@@ -39,7 +39,6 @@
3939
import org.springframework.test.context.ContextConfiguration;
4040
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4141
import org.springframework.transaction.annotation.Transactional;
42-
import org.springframework.util.StringUtils;
4342

4443
/**
4544
* Integration tests for RepositoryMethodsWithEntityGraphConfigJpaRepository.
@@ -62,6 +61,8 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
6261
User christoph;
6362
Role role;
6463

64+
PersistenceUtil util = Persistence.getPersistenceUtil();
65+
6566
@Before
6667
public void setup() {
6768

@@ -91,7 +92,7 @@ public void shouldRespectConfiguredJpaEntityGraph() {
9192
List<User> result = repository.findAll();
9293

9394
assertThat(result.size(), is(3));
94-
assertThat(Persistence.getPersistenceUtil().isLoaded(result.get(0), "roles"), is(true));
95+
assertThat(util.isLoaded(result.get(0), "roles"), is(true));
9596
assertThat(result.get(0), is(tom));
9697
}
9798

@@ -103,8 +104,8 @@ public void shouldRespectConfiguredJpaEntityGraphInFindOne() {
103104
User user = repository.findOne(tom.getId());
104105

105106
assertThat(user, is(notNullValue()));
106-
assertThat("colleages should be fetched with 'user.detail' fetchgraph",
107-
Persistence.getPersistenceUtil().isLoaded(user, "colleagues"), is(true));
107+
assertThat("colleages should be fetched with 'user.detail' fetchgraph", util.isLoaded(user, "colleagues"),
108+
is(true));
108109
}
109110

110111
@Test // DATAJPA-696
@@ -115,8 +116,8 @@ public void shouldRespectInferFetchGraphFromMethodName() {
115116
User user = repository.getOneWithDefinedEntityGraphById(tom.getId());
116117

117118
assertThat(user, is(notNullValue()));
118-
assertThat("colleages should be fetched with 'user.detail' fetchgraph",
119-
Persistence.getPersistenceUtil().isLoaded(user, "colleagues"), is(true));
119+
assertThat("colleages should be fetched with 'user.detail' fetchgraph", util.isLoaded(user, "colleagues"),
120+
is(true));
120121
}
121122

122123
@Test // DATAJPA-696
@@ -130,12 +131,13 @@ public void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {
130131
User user = repository.getOneWithAttributeNamesById(tom.getId());
131132

132133
assertThat(user, is(notNullValue()));
133-
assertThat("colleages should be fetched with 'user.detail' fetchgraph",
134-
Persistence.getPersistenceUtil().isLoaded(user, "colleagues"), is(true));
135-
assertThat(Persistence.getPersistenceUtil().isLoaded(user, "colleagues"), is(true));
134+
135+
assertThat("colleages should be fetched with 'user.detail' fetchgraph", util.isLoaded(user, "colleagues"),
136+
is(true));
137+
assertThat(util.isLoaded(user, "colleagues"), is(true));
136138

137139
for (User colleague : user.getColleagues()) {
138-
assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "roles"), is(true));
140+
assertThat(util.isLoaded(colleague, "roles"), is(true));
139141
}
140142
}
141143

@@ -148,7 +150,7 @@ public void shouldRespectConfiguredJpaEntityGraphWithPaginationAndQueryDslPredic
148150
List<User> result = page.getContent();
149151

150152
assertThat(result.size(), is(3));
151-
assertThat(Persistence.getPersistenceUtil().isLoaded(result.get(0).getRoles()), is(true));
153+
assertThat(util.isLoaded(result.get(0).getRoles()), is(true));
152154
assertThat(result.get(0), is(tom));
153155
}
154156

@@ -165,11 +167,11 @@ public void shouldRespectNamedEntitySubGraph() {
165167
assertThat(user, is(notNullValue()));
166168

167169
assertThat("colleagues on root should have been fetched by named 'User.colleagues' subgraph declaration",
168-
Persistence.getPersistenceUtil().isLoaded(user, "colleagues"), is(true));
170+
util.isLoaded(user, "colleagues"), is(true));
169171

170172
for (User colleague : user.getColleagues()) {
171-
assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "colleagues"), is(true));
172-
assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "roles"), is(true));
173+
assertThat(util.isLoaded(colleague, "colleagues"), is(true));
174+
assertThat(util.isLoaded(colleague, "roles"), is(true));
173175
}
174176
}
175177

@@ -186,55 +188,11 @@ public void shouldRespectMultipleSubGraphForSameAttributeWithDynamicFetchGraph()
186188
assertThat(user, is(notNullValue()));
187189

188190
assertThat("colleagues on root should have been fetched by dynamic subgraph declaration",
189-
Persistence.getPersistenceUtil().isLoaded(user, "colleagues"), is(true));
191+
util.isLoaded(user, "colleagues"), is(true));
190192

191193
for (User colleague : user.getColleagues()) {
192-
assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "colleagues"), is(true));
193-
assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "roles"), is(true));
194-
}
195-
}
196-
197-
@Test // DATAJPA-1041 - TODO: remove when done fighting with eclipselink.
198-
public void thisOneFailsWithEclipselink() {
199-
200-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
201-
202-
em.flush();
203-
em.clear();
204-
205-
javax.persistence.EntityGraph<?> graph = em.getEntityGraph("User.withSubGraph");
206-
207-
printGraph(graph);
208-
209-
User result = (User) em.createQuery("Select u from User u where u.id = " + tom.getId())
210-
.setHint("javax.persistence.loadgraph", graph).getResultList().get(0);
211-
212-
assertThat(Persistence.getPersistenceUtil().isLoaded(result, "roles"), is(true));
213-
assertThat(Persistence.getPersistenceUtil().isLoaded(result, "colleagues"), is(true));
214-
}
215-
216-
private void printGraph(javax.persistence.EntityGraph<?> graph) {
217-
218-
try {
219-
for (AttributeNode<?> node : graph.getAttributeNodes()) {
220-
System.out.println("|- node.getAttributeName(): " + node.getAttributeName());
221-
for (Map.Entry<Class, Subgraph> subGraph : node.getSubgraphs().entrySet()) {
222-
System.out.print("| +- subGraph: " + subGraph.getKey().getSimpleName() + " -> [");
223-
224-
Iterator it = subGraph.getValue().getAttributeNodes().iterator();
225-
while (it.hasNext()) {
226-
227-
AttributeNode<?> an = (AttributeNode<?>) it.next();
228-
System.out.print(an.getAttributeName());
229-
if (it.hasNext()) {
230-
System.out.print(", ");
231-
}
232-
}
233-
System.out.println("]");
234-
}
235-
}
236-
} catch (Exception e) {
237-
// o_O what happened here - ignore it - it's just debug output.
194+
assertThat(util.isLoaded(colleague, "colleagues"), is(true));
195+
assertThat(util.isLoaded(colleague, "roles"), is(true));
238196
}
239197
}
240198
}

src/test/java/org/springframework/data/jpa/repository/sample/RepositoryMethodsWithEntityGraphConfigRepository.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,4 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository
6969
// DATAJPA-1041
7070
@EntityGraph(attributePaths = { "colleagues", "colleagues.roles", "colleagues.colleagues" })
7171
User findOneWithMultipleSubGraphsById(Integer id);
72-
7372
}

0 commit comments

Comments
 (0)