Skip to content
Merged
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 @@ -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;
Expand All @@ -30,7 +30,8 @@
private final String jpaEntityName;

public NamedGraphCreatorJpa(NamedEntityGraph annotation, String jpaEntityName) {
this.name = NullnessHelper.coalesceSuppliedValues( annotation::name, () -> jpaEntityName );
final String name = StringHelper.nullIfEmpty( annotation.name() );

Check notice

Code scanning / CodeQL

Possible confusion of local and field Note

Potentially confusing name:
NamedGraphCreatorJpa
also refers to field
name
(as this.name).
this.name = name == null ? jpaEntityName : name;
this.annotation = annotation;
this.jpaEntityName = jpaEntityName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -61,15 +65,17 @@
final String jpaEntityName = graphContext.typeIndicator().TYPE_NAME().toString();
//noinspection unchecked
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainNameResolver.apply( jpaEntityName );
return GraphParsing.parse( entityDomainType, graphContext.attributeList(), entityNameResolver );
final String name = this.name == null ? jpaEntityName : this.name;

Check notice

Code scanning / CodeQL

Possible confusion of local and field Note

Potentially confusing name: method
createEntityGraph
also refers to field
name
(as this.name).
return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(), entityNameResolver );
}
else {
if ( graphContext.typeIndicator() != null ) {
throw new InvalidGraphException( "Expecting graph text to not include an entity name : " + annotation.graph() );
}
//noinspection unchecked
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainClassResolver.apply( (Class<T>) entityType );
return GraphParsing.parse( entityDomainType, graphContext.attributeList(), entityNameResolver );
final String name = this.name == null ? entityDomainType.getName() : this.name;

Check notice

Code scanning / CodeQL

Possible confusion of local and field Note

Potentially confusing name: method
createEntityGraph
also refers to field
name
(as this.name).
return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(), entityNameResolver );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,7 +121,15 @@ public static <T> RootGraphImplementor<T> parse(
EntityDomainType<T> rootType,
GraphLanguageParser.AttributeListContext attributeListContext,
EntityNameResolver entityNameResolver) {
final RootGraphImpl<T> targetGraph = new RootGraphImpl<>( null, rootType );
return parse( null, rootType, attributeListContext, entityNameResolver );
}

public static <T> RootGraphImplementor<T> parse(
@Nullable String name,
EntityDomainType<T> rootType,
GraphLanguageParser.AttributeListContext attributeListContext,
EntityNameResolver entityNameResolver) {
final RootGraphImpl<T> targetGraph = new RootGraphImpl<>( name, rootType );

final GraphParser visitor = new GraphParser( entityNameResolver );
visitor.getGraphStack().push( targetGraph );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2570,7 +2570,7 @@ public <T> T find(EntityGraph<T> entityGraph, Object primaryKey, FindOption... o
}

private void checkTransactionNeededForLock(LockMode lockMode) {
if ( !LockMode.PESSIMISTIC_READ.greaterThan( lockMode ) ) {
if ( lockMode.greaterThan( LockMode.READ ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, thanks, good catch, @beikov.

checkTransactionNeededForUpdateOperation();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttributeNode<?>> list = graph.getAttributeNodes();
assertThat( list, notNullValue() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down