Skip to content

Commit ba5ba88

Browse files
committed
Ensure EntityGraph#getName returns the name of a named entity graph
1 parent 5bace71 commit ba5ba88

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorJpa.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.hibernate.graph.spi.GraphImplementor;
1515
import org.hibernate.graph.spi.RootGraphImplementor;
1616
import org.hibernate.graph.spi.SubGraphImplementor;
17-
import org.hibernate.internal.util.NullnessHelper;
17+
import org.hibernate.internal.util.StringHelper;
1818
import org.hibernate.metamodel.model.domain.EntityDomainType;
1919

2020
import java.util.function.Function;
@@ -30,7 +30,8 @@ public class NamedGraphCreatorJpa implements NamedGraphCreator {
3030
private final String jpaEntityName;
3131

3232
public NamedGraphCreatorJpa(NamedEntityGraph annotation, String jpaEntityName) {
33-
this.name = NullnessHelper.coalesceSuppliedValues( annotation::name, () -> jpaEntityName );
33+
final String name = StringHelper.nullIfEmpty( annotation.name() );
34+
this.name = name == null ? jpaEntityName : name;
3435
this.annotation = annotation;
3536
this.jpaEntityName = jpaEntityName;
3637
}

hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorParsed.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.antlr.v4.runtime.CharStreams;
88
import org.antlr.v4.runtime.CommonTokenStream;
9+
import org.checkerframework.checker.nullness.qual.Nullable;
910
import org.hibernate.UnknownEntityTypeException;
1011
import org.hibernate.annotations.NamedEntityGraph;
1112
import org.hibernate.grammars.graph.GraphLanguageLexer;
@@ -14,6 +15,7 @@
1415
import org.hibernate.graph.internal.parse.EntityNameResolver;
1516
import org.hibernate.graph.internal.parse.GraphParsing;
1617
import org.hibernate.graph.spi.RootGraphImplementor;
18+
import org.hibernate.internal.util.StringHelper;
1719
import org.hibernate.metamodel.model.domain.EntityDomainType;
1820

1921
import java.util.function.Function;
@@ -22,14 +24,16 @@
2224
* @author Steve Ebersole
2325
*/
2426
public class NamedGraphCreatorParsed implements NamedGraphCreator {
25-
private final Class<?> entityType;
27+
private final @Nullable String name;
28+
private final @Nullable Class<?> entityType;
2629
private final NamedEntityGraph annotation;
2730

2831
public NamedGraphCreatorParsed(NamedEntityGraph annotation) {
2932
this( null, annotation );
3033
}
3134

32-
public NamedGraphCreatorParsed(Class<?> entityType, NamedEntityGraph annotation) {
35+
public NamedGraphCreatorParsed(@Nullable Class<?> entityType, NamedEntityGraph annotation) {
36+
this.name = StringHelper.nullIfEmpty( annotation.name() );
3337
this.entityType = entityType;
3438
this.annotation = annotation;
3539
}
@@ -61,15 +65,17 @@ public <T> EntityDomainType<T> resolveEntityName(String entityName) {
6165
final String jpaEntityName = graphContext.typeIndicator().TYPE_NAME().toString();
6266
//noinspection unchecked
6367
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainNameResolver.apply( jpaEntityName );
64-
return GraphParsing.parse( entityDomainType, graphContext.attributeList(), entityNameResolver );
68+
final String name = this.name == null ? jpaEntityName : this.name;
69+
return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(), entityNameResolver );
6570
}
6671
else {
6772
if ( graphContext.typeIndicator() != null ) {
6873
throw new InvalidGraphException( "Expecting graph text to not include an entity name : " + annotation.graph() );
6974
}
7075
//noinspection unchecked
7176
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainClassResolver.apply( (Class<T>) entityType );
72-
return GraphParsing.parse( entityDomainType, graphContext.attributeList(), entityNameResolver );
77+
final String name = this.name == null ? entityDomainType.getName() : this.name;
78+
return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(), entityNameResolver );
7379
}
7480
}
7581
}

hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParsing.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.antlr.v4.runtime.CharStreams;
88
import org.antlr.v4.runtime.CommonTokenStream;
9+
import org.checkerframework.checker.nullness.qual.Nullable;
910
import org.hibernate.engine.spi.SessionFactoryImplementor;
1011
import org.hibernate.grammars.graph.GraphLanguageLexer;
1112
import org.hibernate.grammars.graph.GraphLanguageParser;
@@ -120,7 +121,15 @@ public static <T> RootGraphImplementor<T> parse(
120121
EntityDomainType<T> rootType,
121122
GraphLanguageParser.AttributeListContext attributeListContext,
122123
EntityNameResolver entityNameResolver) {
123-
final RootGraphImpl<T> targetGraph = new RootGraphImpl<>( null, rootType );
124+
return parse( null, rootType, attributeListContext, entityNameResolver );
125+
}
126+
127+
public static <T> RootGraphImplementor<T> parse(
128+
@Nullable String name,
129+
EntityDomainType<T> rootType,
130+
GraphLanguageParser.AttributeListContext attributeListContext,
131+
EntityNameResolver entityNameResolver) {
132+
final RootGraphImpl<T> targetGraph = new RootGraphImpl<>( name, rootType );
124133

125134
final GraphParser visitor = new GraphParser( entityNameResolver );
126135
visitor.getGraphStack().push( targetGraph );

hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -813,15 +813,15 @@ public static String[] unquote(final String[] names, final Dialect dialect) {
813813
}
814814
}
815815

816-
public static String nullIfEmpty(@Nullable String value) {
816+
public static @Nullable String nullIfEmpty(@Nullable String value) {
817817
return isEmpty( value ) ? null : value;
818818
}
819819

820-
public static String nullIfBlank(@Nullable String value) {
820+
public static @Nullable String nullIfBlank(@Nullable String value) {
821821
return isBlank( value ) ? null : value;
822822
}
823823

824-
public static String subStringNullIfEmpty(String value, Character startChar) {
824+
public static @Nullable String subStringNullIfEmpty(String value, Character startChar) {
825825
if ( isEmpty( value ) ) {
826826
return null;
827827
}
@@ -831,7 +831,7 @@ public static String subStringNullIfEmpty(String value, Character startChar) {
831831
}
832832
}
833833

834-
public static String[] splitAtCommas(String incomingString) {
834+
public static String[] splitAtCommas(@Nullable String incomingString) {
835835
return incomingString==null || incomingString.isBlank()
836836
? EMPTY_STRINGS
837837
: COMMA_SEPARATED_PATTERN.split( incomingString );

hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/multiple/NamedEntityGraphsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void testAttributeNodesAreAvailable(SessionFactoryScope scope) {
5757
EntityManager em = session.unwrap( EntityManager.class );
5858
EntityGraph<?> graph = em.getEntityGraph( "name_salary_graph" );
5959
assertThat( graph, notNullValue() );
60+
assertThat( graph.getName(), is( "name_salary_graph" ) );
6061

6162
List<AttributeNode<?>> list = graph.getAttributeNodes();
6263
assertThat( list, notNullValue() );

hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/parsed/PackageLevelTests.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.hibernate.boot.registry.StandardServiceRegistry;
1010
import org.hibernate.engine.spi.SessionFactoryImplementor;
1111
import org.hibernate.graph.InvalidGraphException;
12+
import org.hibernate.graph.spi.RootGraphImplementor;
1213
import org.hibernate.orm.test.entitygraph.named.parsed.pkg.Book;
1314
import org.hibernate.orm.test.entitygraph.named.parsed.pkg.Duplicator;
1415
import org.hibernate.orm.test.entitygraph.named.parsed.pkg.Isbn;
@@ -22,6 +23,7 @@
2223
import org.junit.jupiter.api.Test;
2324

2425
import static org.hibernate.orm.test.entitygraph.parser.AssertionHelper.assertBasicAttributes;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
2527
import static org.junit.jupiter.api.Assertions.fail;
2628

2729
/**
@@ -38,9 +40,15 @@ public class PackageLevelTests {
3840
void testDiscovery(SessionFactoryScope factoryScope) {
3941
final SessionFactoryImplementor sessionFactory = factoryScope.getSessionFactory();
4042

41-
assertBasicAttributes( sessionFactory.findEntityGraphByName( "book-title-isbn" ), "title", "isbn" );
42-
assertBasicAttributes( sessionFactory.findEntityGraphByName( "book-title-isbn-author" ), "title", "isbn", "author" );
43-
assertBasicAttributes( sessionFactory.findEntityGraphByName( "book-title-isbn-editor" ), "title", "isbn", "editor" );
43+
assertBasicGraph( sessionFactory, "book-title-isbn", "title", "isbn" );
44+
assertBasicGraph( sessionFactory, "book-title-isbn-author", "title", "isbn", "author" );
45+
assertBasicGraph( sessionFactory, "book-title-isbn-editor", "title", "isbn", "editor" );
46+
}
47+
48+
private static void assertBasicGraph(SessionFactoryImplementor sessionFactory, String name, String... names) {
49+
RootGraphImplementor<?> graph = sessionFactory.findEntityGraphByName( name );
50+
assertEquals( name, graph.getName() );
51+
assertBasicAttributes( graph, names );
4452
}
4553

4654
@Test

0 commit comments

Comments
 (0)