diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorJpa.java b/hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorJpa.java index b695eda6c5ff..dd919bfd1f3c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorJpa.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorJpa.java @@ -14,7 +14,7 @@ import org.hibernate.graph.spi.GraphImplementor; import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.graph.spi.SubGraphImplementor; -import org.hibernate.internal.util.NullnessHelper; +import org.hibernate.internal.util.StringHelper; import org.hibernate.metamodel.model.domain.EntityDomainType; import java.util.function.Function; @@ -30,7 +30,8 @@ public class NamedGraphCreatorJpa implements NamedGraphCreator { private final String jpaEntityName; public NamedGraphCreatorJpa(NamedEntityGraph annotation, String jpaEntityName) { - this.name = NullnessHelper.coalesceSuppliedValues( annotation::name, () -> jpaEntityName ); + final String name = StringHelper.nullIfEmpty( annotation.name() ); + this.name = name == null ? jpaEntityName : name; this.annotation = annotation; this.jpaEntityName = jpaEntityName; } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorParsed.java b/hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorParsed.java index 99e2b934a98c..38b897226a60 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorParsed.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreatorParsed.java @@ -6,6 +6,7 @@ import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; +import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.UnknownEntityTypeException; import org.hibernate.annotations.NamedEntityGraph; import org.hibernate.grammars.graph.GraphLanguageLexer; @@ -14,6 +15,7 @@ import org.hibernate.graph.internal.parse.EntityNameResolver; import org.hibernate.graph.internal.parse.GraphParsing; import org.hibernate.graph.spi.RootGraphImplementor; +import org.hibernate.internal.util.StringHelper; import org.hibernate.metamodel.model.domain.EntityDomainType; import java.util.function.Function; @@ -22,14 +24,16 @@ * @author Steve Ebersole */ public class NamedGraphCreatorParsed implements NamedGraphCreator { - private final Class entityType; + private final @Nullable String name; + private final @Nullable Class entityType; private final NamedEntityGraph annotation; public NamedGraphCreatorParsed(NamedEntityGraph annotation) { this( null, annotation ); } - public NamedGraphCreatorParsed(Class entityType, NamedEntityGraph annotation) { + public NamedGraphCreatorParsed(@Nullable Class entityType, NamedEntityGraph annotation) { + this.name = StringHelper.nullIfEmpty( annotation.name() ); this.entityType = entityType; this.annotation = annotation; } @@ -61,7 +65,8 @@ public EntityDomainType resolveEntityName(String entityName) { final String jpaEntityName = graphContext.typeIndicator().TYPE_NAME().toString(); //noinspection unchecked final EntityDomainType entityDomainType = (EntityDomainType) entityDomainNameResolver.apply( jpaEntityName ); - return GraphParsing.parse( entityDomainType, graphContext.attributeList(), entityNameResolver ); + final String name = this.name == null ? jpaEntityName : this.name; + return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(), entityNameResolver ); } else { if ( graphContext.typeIndicator() != null ) { @@ -69,7 +74,8 @@ public EntityDomainType resolveEntityName(String entityName) { } //noinspection unchecked final EntityDomainType entityDomainType = (EntityDomainType) entityDomainClassResolver.apply( (Class) entityType ); - return GraphParsing.parse( entityDomainType, graphContext.attributeList(), entityNameResolver ); + final String name = this.name == null ? entityDomainType.getName() : this.name; + return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(), entityNameResolver ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParsing.java b/hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParsing.java index 919cc33c7c3f..2c8be74ef601 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParsing.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParsing.java @@ -6,6 +6,7 @@ import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; +import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.grammars.graph.GraphLanguageLexer; import org.hibernate.grammars.graph.GraphLanguageParser; @@ -120,7 +121,15 @@ public static RootGraphImplementor parse( EntityDomainType rootType, GraphLanguageParser.AttributeListContext attributeListContext, EntityNameResolver entityNameResolver) { - final RootGraphImpl targetGraph = new RootGraphImpl<>( null, rootType ); + return parse( null, rootType, attributeListContext, entityNameResolver ); + } + + public static RootGraphImplementor parse( + @Nullable String name, + EntityDomainType rootType, + GraphLanguageParser.AttributeListContext attributeListContext, + EntityNameResolver entityNameResolver) { + final RootGraphImpl targetGraph = new RootGraphImpl<>( name, rootType ); final GraphParser visitor = new GraphParser( entityNameResolver ); visitor.getGraphStack().push( targetGraph ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 09274592ec8a..fa3adddecc72 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -2570,7 +2570,7 @@ public T find(EntityGraph entityGraph, Object primaryKey, FindOption... o } private void checkTransactionNeededForLock(LockMode lockMode) { - if ( !LockMode.PESSIMISTIC_READ.greaterThan( lockMode ) ) { + if ( lockMode.greaterThan( LockMode.READ ) ) { checkTransactionNeededForUpdateOperation(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java index 632625d97327..16cd1a35b1cb 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java @@ -813,15 +813,15 @@ public static String[] unquote(final String[] names, final Dialect dialect) { } } - public static String nullIfEmpty(@Nullable String value) { + public static @Nullable String nullIfEmpty(@Nullable String value) { return isEmpty( value ) ? null : value; } - public static String nullIfBlank(@Nullable String value) { + public static @Nullable String nullIfBlank(@Nullable String value) { return isBlank( value ) ? null : value; } - public static String subStringNullIfEmpty(String value, Character startChar) { + public static @Nullable String subStringNullIfEmpty(String value, Character startChar) { if ( isEmpty( value ) ) { return null; } @@ -831,7 +831,7 @@ public static String subStringNullIfEmpty(String value, Character startChar) { } } - public static String[] splitAtCommas(String incomingString) { + public static String[] splitAtCommas(@Nullable String incomingString) { return incomingString==null || incomingString.isBlank() ? EMPTY_STRINGS : COMMA_SEPARATED_PATTERN.split( incomingString ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/multiple/NamedEntityGraphsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/multiple/NamedEntityGraphsTest.java index 1e797ed8f588..1aebfb18cc10 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/multiple/NamedEntityGraphsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/multiple/NamedEntityGraphsTest.java @@ -57,6 +57,7 @@ void testAttributeNodesAreAvailable(SessionFactoryScope scope) { EntityManager em = session.unwrap( EntityManager.class ); EntityGraph graph = em.getEntityGraph( "name_salary_graph" ); assertThat( graph, notNullValue() ); + assertThat( graph.getName(), is( "name_salary_graph" ) ); List> list = graph.getAttributeNodes(); assertThat( list, notNullValue() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/parsed/PackageLevelTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/parsed/PackageLevelTests.java index 7f0a7e7f2750..97ef1080c219 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/parsed/PackageLevelTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/named/parsed/PackageLevelTests.java @@ -9,6 +9,7 @@ import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.graph.InvalidGraphException; +import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.orm.test.entitygraph.named.parsed.pkg.Book; import org.hibernate.orm.test.entitygraph.named.parsed.pkg.Duplicator; import org.hibernate.orm.test.entitygraph.named.parsed.pkg.Isbn; @@ -22,6 +23,7 @@ import org.junit.jupiter.api.Test; import static org.hibernate.orm.test.entitygraph.parser.AssertionHelper.assertBasicAttributes; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; /** @@ -38,9 +40,15 @@ public class PackageLevelTests { void testDiscovery(SessionFactoryScope factoryScope) { final SessionFactoryImplementor sessionFactory = factoryScope.getSessionFactory(); - assertBasicAttributes( sessionFactory.findEntityGraphByName( "book-title-isbn" ), "title", "isbn" ); - assertBasicAttributes( sessionFactory.findEntityGraphByName( "book-title-isbn-author" ), "title", "isbn", "author" ); - assertBasicAttributes( sessionFactory.findEntityGraphByName( "book-title-isbn-editor" ), "title", "isbn", "editor" ); + assertBasicGraph( sessionFactory, "book-title-isbn", "title", "isbn" ); + assertBasicGraph( sessionFactory, "book-title-isbn-author", "title", "isbn", "author" ); + assertBasicGraph( sessionFactory, "book-title-isbn-editor", "title", "isbn", "editor" ); + } + + private static void assertBasicGraph(SessionFactoryImplementor sessionFactory, String name, String... names) { + RootGraphImplementor graph = sessionFactory.findEntityGraphByName( name ); + assertEquals( name, graph.getName() ); + assertBasicAttributes( graph, names ); } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/FlushAndTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/FlushAndTransactionTest.java index 79ea55e51071..71968496f37d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/FlushAndTransactionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/FlushAndTransactionTest.java @@ -48,9 +48,8 @@ public void testAlwaysTransactionalOperations() { catch (TransactionRequiredException e) { //success } - em.lock( book, LockModeType.READ ); try { - em.lock( book, LockModeType.PESSIMISTIC_READ ); + em.lock( book, LockModeType.READ ); fail( "lock has to be inside a Tx" ); } catch (TransactionRequiredException e) {