Skip to content

Lift restriction on empty JpaEntityGraph attributes #3684

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 3 commits 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.5.x-GH-3682-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.5.x-GH-3682-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.5.x-GH-3682-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.5.x-GH-3682-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.5.x-GH-3682-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.5.x-GH-3682-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ private static EntityGraph<?> createDynamicEntityGraph(EntityManager em, JpaEnti
Assert.notNull(em, "EntityManager must not be null");
Assert.notNull(jpaEntityGraph, "JpaEntityGraph must not be null");
Assert.notNull(entityType, "Entity type must not be null");
Assert.isTrue(jpaEntityGraph.isAdHocEntityGraph(), "The given " + jpaEntityGraph + " is not dynamic");

EntityGraph<?> entityGraph = em.createEntityGraph(entityType);
configureFetchGraphFrom(jpaEntityGraph, entityGraph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.springframework.data.jpa.repository.query;

import java.util.Arrays;
import java.util.List;

import org.springframework.data.jpa.repository.EntityGraph;
Expand All @@ -29,12 +28,11 @@
*
* @author Thomas Darimont
* @author Mark Paluch
* @author Christoph Strobl
* @since 1.6
*/
public class JpaEntityGraph {

private static String[] EMPTY_ATTRIBUTE_PATHS = {};

private final String name;
private final EntityGraphType type;
private final List<String> attributePaths;
Expand All @@ -46,8 +44,8 @@ public class JpaEntityGraph {
* @param nameFallback must not be {@literal null} or empty.
*/
public JpaEntityGraph(EntityGraph entityGraph, String nameFallback) {
this(StringUtils.hasText(entityGraph.value()) ? entityGraph.value() : nameFallback, entityGraph.type(), entityGraph
.attributePaths());
this(StringUtils.hasText(entityGraph.value()) ? entityGraph.value() : nameFallback, entityGraph.type(),
entityGraph.attributePaths());
}

/**
Expand All @@ -65,7 +63,7 @@ public JpaEntityGraph(String name, EntityGraphType type, @Nullable String[] attr

this.name = name;
this.type = type;
this.attributePaths = Arrays.asList(attributePaths == null ? EMPTY_ATTRIBUTE_PATHS : attributePaths);
this.attributePaths = attributePaths != null ? List.of(attributePaths) : List.of();
}

/**
Expand Down Expand Up @@ -99,9 +97,11 @@ public List<String> getAttributePaths() {
/**
* Return {@literal true} if this {@link JpaEntityGraph} needs to be generated on-the-fly.
*
* @return
* @return {@literal true} if {@link #attributePaths} is not empty.
* @since 1.9
* @deprecated since 3.5 as the used evaluation does not represent wether a {@link JpaEntityGraph} is dynamic or not.
*/
@Deprecated(since = "3.5", forRemoval = true)
public boolean isAdHocEntityGraph() {
return !attributePaths.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Mark Paluch
* @author Jens Schauder
* @author Krzysztof Krason
* @author Christoph Strobl
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:application-context.xml")
Expand Down Expand Up @@ -166,6 +167,15 @@ void errorsOnUnknownProperties() {
em.createEntityGraph(User.class)));
}

@Test // GH-3682
void allowsEmptyGraph() {

EntityGraph<User> graph = em.createEntityGraph(User.class);
Jpa21Utils.configureFetchGraphFrom(new JpaEntityGraph("User.NoNamedEntityGraphAvailable", EntityGraphType.FETCH, new String[0]), graph);

Assertions.assertThat(graph.getAttributeNodes()).isEmpty();
}

/**
* Lookup the {@link AttributeNode} with given {@literal nodeName} in the root of the given {@literal graph}.
*/
Expand Down