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
23 changes: 9 additions & 14 deletions hibernate-core/src/main/java/org/hibernate/SessionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import jakarta.persistence.TypedQueryReference;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.graph.GraphParser;
import org.hibernate.graph.InvalidGraphException;
import org.hibernate.graph.RootGraph;
Expand Down Expand Up @@ -108,7 +107,7 @@
* SingularAttribute<Book,String>}.
* </ul>
* <p>
* Use of these statically-typed metamodel references is the preferred way of
* Use of these statically typed metamodel references is the preferred way of
* working with the {@linkplain jakarta.persistence.criteria.CriteriaBuilder
* criteria query API}, and with {@linkplain EntityGraph}s.
* <p>
Expand Down Expand Up @@ -266,7 +265,7 @@ default void inSession(Consumer<? super Session> action) {
* @since 6.3
*/
default void inStatelessSession(Consumer<? super StatelessSession> action) {
try ( StatelessSession session = openStatelessSession() ) {
try ( var session = openStatelessSession() ) {
action.accept( session );
}
}
Expand Down Expand Up @@ -308,7 +307,7 @@ default void inStatelessTransaction(Consumer<? super StatelessSession> action) {
* @see #fromTransaction(Function)
*/
default <R> R fromSession(Function<? super Session,R> action) {
try ( Session session = openSession() ) {
try ( var session = openSession() ) {
return action.apply( session );
}
}
Expand All @@ -331,7 +330,7 @@ default <R> R fromSession(Function<? super Session,R> action) {
* @since 6.3
*/
default <R> R fromStatelessSession(Function<? super StatelessSession,R> action) {
try ( StatelessSession session = openStatelessSession() ) {
try ( var session = openStatelessSession() ) {
return action.apply( session );
}
}
Expand Down Expand Up @@ -522,9 +521,7 @@ default <T> RootGraph<T> createEntityGraph(Class<T> entityType) {
*
* @since 7.0
*/
default <T> RootGraph<T> parseEntityGraph(Class<T> rootEntityClass, CharSequence graphText) {
return GraphParser.parse( rootEntityClass, graphText.toString(), unwrap( SessionFactoryImplementor.class ) );
}
<T> RootGraph<T> parseEntityGraph(Class<T> rootEntityClass, CharSequence graphText);

/**
* Creates a {@link RootGraph} for the given {@code rootEntityName} and parses the graph
Expand All @@ -543,9 +540,8 @@ default <T> RootGraph<T> parseEntityGraph(Class<T> rootEntityClass, CharSequence
*
* @since 7.0
*/
default <T> RootGraph<T> parseEntityGraph(String rootEntityName, CharSequence graphText) {
return GraphParser.parse( rootEntityName, graphText.toString(), unwrap( SessionFactoryImplementor.class ) );
}
@Incubating
<T> RootGraph<T> parseEntityGraph(String rootEntityName, CharSequence graphText);

/**
* Creates a {@link RootGraph} based on the passed string representation. Here, the
Expand All @@ -560,9 +556,8 @@ default <T> RootGraph<T> parseEntityGraph(String rootEntityName, CharSequence gr
*
* @since 7.0
*/
default <T> RootGraph<T> parseEntityGraph(CharSequence graphText) {
return GraphParser.parse( graphText.toString(), unwrap( SessionFactoryImplementor.class ) );
}
@Incubating
<T> RootGraph<T> parseEntityGraph(CharSequence graphText);

/**
* Obtain the set of names of all {@linkplain org.hibernate.annotations.FilterDef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
*/
package org.hibernate.boot.model;

import org.hibernate.graph.spi.GraphParserEntityClassResolver;
import org.hibernate.graph.spi.GraphParserEntityNameResolver;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.metamodel.model.domain.EntityDomainType;

import java.util.function.Function;

/**
* @author Steve Ebersole
*/
@FunctionalInterface
public interface NamedGraphCreator {
<T> RootGraphImplementor<T> createEntityGraph(
Function<Class<T>, EntityDomainType<?>> entityDomainClassResolver,
Function<String, EntityDomainType<?>> entityDomainNameResolver);
RootGraphImplementor<?> createEntityGraph(
GraphParserEntityClassResolver entityDomainClassResolver,
GraphParserEntityNameResolver entityDomainNameResolver);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@

import jakarta.persistence.NamedAttributeNode;
import jakarta.persistence.NamedEntityGraph;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.hibernate.AnnotationException;
import org.hibernate.boot.model.NamedGraphCreator;
import org.hibernate.graph.internal.RootGraphImpl;
import org.hibernate.graph.spi.AttributeNodeImplementor;
import org.hibernate.graph.spi.GraphParserEntityClassResolver;
import org.hibernate.graph.spi.GraphParserEntityNameResolver;
import org.hibernate.graph.spi.GraphImplementor;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.graph.spi.SubGraphImplementor;
import org.hibernate.metamodel.model.domain.EntityDomainType;

import java.util.function.Function;

import static org.hibernate.internal.util.StringHelper.isNotEmpty;
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;

Expand All @@ -36,16 +37,16 @@ class NamedGraphCreatorJpa implements NamedGraphCreator {
}

@Override
public <T> RootGraphImplementor<T> createEntityGraph(
Function<Class<T>, EntityDomainType<?>> entityDomainClassResolver,
Function<String, EntityDomainType<?>> entityDomainNameResolver) {
//noinspection unchecked
final var rootEntityType =
(EntityDomainType<T>)
entityDomainNameResolver.apply( jpaEntityName );
public RootGraphImplementor<?> createEntityGraph(
GraphParserEntityClassResolver entityDomainClassResolver,
GraphParserEntityNameResolver entityDomainNameResolver) {
return createGraph( (EntityDomainType<?>)
entityDomainNameResolver.resolveEntityName( jpaEntityName ) );
}

private <T> @NonNull RootGraphImplementor<T> createGraph(EntityDomainType<T> rootEntityType) {
final var entityGraph =
createRootGraph( name, rootEntityType, annotation.includeAllAttributes() );

final var subclassSubgraphs = annotation.subclassSubgraphs();
if ( subclassSubgraphs != null ) {
for ( var subclassSubgraph : subclassSubgraphs ) {
Expand All @@ -59,11 +60,9 @@ public <T> RootGraphImplementor<T> createEntityGraph(
entityGraph.addTreatedSubgraph( subgraphType.asSubclass( graphJavaType ) ) );
}
}

if ( annotation.attributeNodes() != null ) {
applyNamedAttributeNodes( annotation.attributeNodes(), annotation, entityGraph );
}

return entityGraph;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.UnknownEntityTypeException;
import org.hibernate.annotations.NamedEntityGraph;
import org.hibernate.boot.model.NamedGraphCreator;
import org.hibernate.grammars.graph.GraphLanguageLexer;
import org.hibernate.grammars.graph.GraphLanguageParser;
import org.hibernate.graph.InvalidGraphException;
import org.hibernate.graph.internal.parse.EntityNameResolver;
import org.hibernate.graph.spi.GraphParserEntityClassResolver;
import org.hibernate.graph.spi.GraphParserEntityNameResolver;
import org.hibernate.graph.internal.parse.GraphParsing;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.metamodel.model.domain.EntityDomainType;

import java.util.function.Function;

import static org.hibernate.internal.util.StringHelper.nullIfEmpty;

/**
Expand All @@ -41,43 +41,52 @@
}

@Override
public <T> RootGraphImplementor<T> createEntityGraph(
Function<Class<T>, EntityDomainType<?>> entityDomainClassResolver,
Function<String, EntityDomainType<?>> entityDomainNameResolver) {
final GraphLanguageLexer lexer = new GraphLanguageLexer( CharStreams.fromString( annotation.graph() ) );
final GraphLanguageParser parser = new GraphLanguageParser( new CommonTokenStream( lexer ) );
final GraphLanguageParser.GraphContext graphContext = parser.graph();

final EntityNameResolver entityNameResolver = new EntityNameResolver() {
@Override
public <T> EntityDomainType<T> resolveEntityName(String entityName) {
//noinspection unchecked
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainNameResolver.apply( entityName );
if ( entityDomainType != null ) {
return entityDomainType;
}
throw new UnknownEntityTypeException( entityName );
}
};
public RootGraphImplementor<?> createEntityGraph(
GraphParserEntityClassResolver entityDomainClassResolver,
GraphParserEntityNameResolver entityDomainNameResolver) {
final var lexer = new GraphLanguageLexer( CharStreams.fromString( annotation.graph() ) );
final var parser = new GraphLanguageParser( new CommonTokenStream( lexer ) );
final var graphContext = parser.graph();

final var typeIndicator = graphContext.typeIndicator();
if ( entityType == null ) {
if ( graphContext.typeIndicator() == null ) {
if ( typeIndicator == null ) {
throw new InvalidGraphException( "Expecting graph text to include an entity name : " + annotation.graph() );
}
final String jpaEntityName = graphContext.typeIndicator().TYPE_NAME().toString();
//noinspection unchecked
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainNameResolver.apply( jpaEntityName );
final String jpaEntityName = typeIndicator.TYPE_NAME().toString();
final var entityDomainType = entityDomainNameResolver.resolveEntityName( jpaEntityName );
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 );
return parse( entityDomainNameResolver, name, entityDomainType, graphContext );
}
else {
if ( graphContext.typeIndicator() != null ) {
if ( 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 );
final var entityDomainType = entityDomainClassResolver.resolveEntityClass( entityType );
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 );
return parse( entityDomainNameResolver, name, entityDomainType, graphContext );
}
}

private static @NonNull RootGraphImplementor<?> parse(
GraphParserEntityNameResolver entityDomainNameResolver,
String name,
EntityDomainType<?> entityDomainType,
GraphLanguageParser.GraphContext graphContext) {
return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(),
entityName -> resolve( entityName, entityDomainNameResolver ) );
}

private static @NonNull EntityDomainType<?> resolve(
String entityName, GraphParserEntityNameResolver entityDomainNameResolver) {
final var entityDomainType =
(EntityDomainType<?>)
entityDomainNameResolver.resolveEntityName( entityName );
if ( entityDomainType == null ) {
throw new UnknownEntityTypeException( entityName );
}
else {
return entityDomainType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static Identifier uniqueKeyName(MetadataBuildingContext context, Annotat

private static void addColumnsToUniqueKey(AnnotatedColumns columns, Identifier name) {
final var collector = columns.getBuildingContext().getMetadataCollector();
final Table table = columns.getTable();
final var table = columns.getTable();
final var uniqueKey = table.getOrCreateUniqueKey( name.render( collector.getDatabase().getDialect() ) );
final var property = columns.resolveProperty();
if ( property.isComposite() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EntityCopyObserverFactory;
import org.hibernate.event.spi.EventEngine;
import org.hibernate.graph.GraphParser;
import org.hibernate.graph.RootGraph;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.event.service.spi.EventListenerGroups;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
Expand Down Expand Up @@ -322,4 +324,18 @@ default JdbcSelectWithActionsBuilder getJdbcSelectWithActionsBuilder(){
return new JdbcSelectWithActions.Builder();
}

@Override
default <T> RootGraph<T> parseEntityGraph(Class<T> rootEntityClass, CharSequence graphText) {
return GraphParser.parse( rootEntityClass, graphText.toString(), unwrap( SessionFactoryImplementor.class ) );
}

@Override @Incubating
default <T> RootGraph<T> parseEntityGraph(String rootEntityName, CharSequence graphText) {
return GraphParser.parse( rootEntityName, graphText.toString(), unwrap( SessionFactoryImplementor.class ) );
}

@Override @Incubating
default <T> RootGraph<T> parseEntityGraph(CharSequence graphText) {
return GraphParser.parse( graphText.toString(), unwrap( SessionFactoryImplementor.class ) );
}
}
Loading
Loading