Skip to content

Commit 55580c7

Browse files
committed
Fixed EntityGraphFactory overwriting subgraphs
Closes #2527
1 parent 63919b5 commit 55580c7

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/EntityGraphFactory.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.jpa.repository.support;
1717

18+
import java.util.HashMap;
19+
import java.util.Map;
1820
import java.util.Set;
1921

2022
import jakarta.persistence.EntityGraph;
@@ -27,6 +29,7 @@
2729
* Factory class to create an {@link EntityGraph} from a collection of property paths.
2830
*
2931
* @author Jens Schauder
32+
* @author Petr Strnad
3033
* @since 2.6
3134
*/
3235
abstract class EntityGraphFactory {
@@ -42,16 +45,20 @@ abstract class EntityGraphFactory {
4245
public static <T> EntityGraph<T> create(EntityManager entityManager, Class<T> domainType, Set<String> properties) {
4346

4447
EntityGraph<T> entityGraph = entityManager.createEntityGraph(domainType);
48+
Map<String, Subgraph<Object>> existingSubgraphs = new HashMap<>();
4549

4650
for (String property : properties) {
4751

4852
Subgraph<Object> current = null;
53+
String currentFullPath = "";
4954

5055
for (PropertyPath path : PropertyPath.from(property, domainType)) {
5156

57+
currentFullPath += path.getSegment() + ".";
5258
if (path.hasNext()) {
53-
current = current == null ? entityGraph.addSubgraph(path.getSegment())
54-
: current.addSubgraph(path.getSegment());
59+
final Subgraph<Object> finalCurrent = current;
60+
current = current == null ? existingSubgraphs.computeIfAbsent(currentFullPath, k -> entityGraph.addSubgraph(path.getSegment()))
61+
: existingSubgraphs.computeIfAbsent(currentFullPath, k -> finalCurrent.addSubgraph(path.getSegment()));
5562
continue;
5663
}
5764

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/EntityGraphFactoryUnitTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Unit tests for {@link EntityGraphFactory}.
3232
*
3333
* @author Jens Schauder
34+
* @author Petr Strnad
3435
*/
3536
@SuppressWarnings("rawtypes")
3637
class EntityGraphFactoryUnitTests {
@@ -61,12 +62,13 @@ void simpleSetOfPropertiesGetRegistered() {
6162
@Test
6263
void setOfCompositePropertiesGetRegisteredPiecewise() {
6364

64-
HashSet<String> properties = new HashSet<>(asList("one.two", "eins.zwei.drei"));
65+
HashSet<String> properties = new HashSet<>(asList("one.one", "one.two", "eins.zwei.drei"));
6566

6667
entityGraph = EntityGraphFactory.create(em, DummyEntity.class, properties);
6768

6869
verify(entityGraph).addSubgraph("one");
6970
Subgraph<?> one = entityGraph.addSubgraph("one");
71+
verify(one).addAttributeNodes("one");
7072
verify(one).addAttributeNodes("two");
7173

7274
verify(entityGraph).addSubgraph("eins");

0 commit comments

Comments
 (0)