File tree 2 files changed +12
-3
lines changed
main/java/org/springframework/data/jpa/repository/support
test/java/org/springframework/data/jpa/repository/support
2 files changed +12
-3
lines changed Original file line number Diff line number Diff line change 15
15
*/
16
16
package org .springframework .data .jpa .repository .support ;
17
17
18
+ import java .util .HashMap ;
19
+ import java .util .Map ;
18
20
import java .util .Set ;
19
21
20
22
import jakarta .persistence .EntityGraph ;
27
29
* Factory class to create an {@link EntityGraph} from a collection of property paths.
28
30
*
29
31
* @author Jens Schauder
32
+ * @author Petr Strnad
30
33
* @since 2.6
31
34
*/
32
35
abstract class EntityGraphFactory {
@@ -42,16 +45,20 @@ abstract class EntityGraphFactory {
42
45
public static <T > EntityGraph <T > create (EntityManager entityManager , Class <T > domainType , Set <String > properties ) {
43
46
44
47
EntityGraph <T > entityGraph = entityManager .createEntityGraph (domainType );
48
+ Map <String , Subgraph <Object >> existingSubgraphs = new HashMap <>();
45
49
46
50
for (String property : properties ) {
47
51
48
52
Subgraph <Object > current = null ;
53
+ String currentFullPath = "" ;
49
54
50
55
for (PropertyPath path : PropertyPath .from (property , domainType )) {
51
56
57
+ currentFullPath += path .getSegment () + "." ;
52
58
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 ()));
55
62
continue ;
56
63
}
57
64
Original file line number Diff line number Diff line change 31
31
* Unit tests for {@link EntityGraphFactory}.
32
32
*
33
33
* @author Jens Schauder
34
+ * @author Petr Strnad
34
35
*/
35
36
@ SuppressWarnings ("rawtypes" )
36
37
class EntityGraphFactoryUnitTests {
@@ -61,12 +62,13 @@ void simpleSetOfPropertiesGetRegistered() {
61
62
@ Test
62
63
void setOfCompositePropertiesGetRegisteredPiecewise () {
63
64
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" ));
65
66
66
67
entityGraph = EntityGraphFactory .create (em , DummyEntity .class , properties );
67
68
68
69
verify (entityGraph ).addSubgraph ("one" );
69
70
Subgraph <?> one = entityGraph .addSubgraph ("one" );
71
+ verify (one ).addAttributeNodes ("one" );
70
72
verify (one ).addAttributeNodes ("two" );
71
73
72
74
verify (entityGraph ).addSubgraph ("eins" );
You can’t perform that action at this time.
0 commit comments