Skip to content

Fixed EntityGraphFactory overwriting subgraphs #2571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.jpa.repository.support;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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

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

for (String property : properties) {

Subgraph<Object> current = null;
String currentFullPath = "";

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

currentFullPath += path.getSegment() + ".";
if (path.hasNext()) {
current = current == null ? entityGraph.addSubgraph(path.getSegment())
: current.addSubgraph(path.getSegment());
final Subgraph<Object> finalCurrent = current;
current = current == null ? existingSubgraphs.computeIfAbsent(currentFullPath, k -> entityGraph.addSubgraph(path.getSegment()))
: existingSubgraphs.computeIfAbsent(currentFullPath, k -> finalCurrent.addSubgraph(path.getSegment()));
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Unit tests for {@link EntityGraphFactory}.
*
* @author Jens Schauder
* @author Petr Strnad
*/
@SuppressWarnings("rawtypes")
class EntityGraphFactoryUnitTests {
Expand Down Expand Up @@ -61,12 +62,13 @@ void simpleSetOfPropertiesGetRegistered() {
@Test
void setOfCompositePropertiesGetRegisteredPiecewise() {

HashSet<String> properties = new HashSet<>(asList("one.two", "eins.zwei.drei"));
HashSet<String> properties = new HashSet<>(asList("one.one", "one.two", "eins.zwei.drei"));

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

verify(entityGraph).addSubgraph("one");
Subgraph<?> one = entityGraph.addSubgraph("one");
verify(one).addAttributeNodes("one");
verify(one).addAttributeNodes("two");

verify(entityGraph).addSubgraph("eins");
Expand Down