make it possible to get a named EntityGraph without an unchecked typecast

This commit is contained in:
Gavin King 2023-07-13 16:19:44 +02:00
parent 35ea74ec54
commit af7e7b9afc
5 changed files with 54 additions and 0 deletions

View File

@ -246,6 +246,8 @@ public interface SharedSessionContract extends QueryProducer, Closeable, Seriali
/**
* Create a new mutable {@link EntityGraph} with only a root node.
*
* @param rootType the root entity class of the graph
*
* @since 6.3
*/
<T> RootGraph<T> createEntityGraph(Class<T> rootType);
@ -263,6 +265,23 @@ public interface SharedSessionContract extends QueryProducer, Closeable, Seriali
*/
RootGraph<?> createEntityGraph(String graphName);
/**
* Create a new mutable copy of the named {@link EntityGraph},
* or return {@code null} if there is no graph with the given
* name.
*
* @param rootType the root entity class of the graph
* @param graphName the name of the graph
*
* @see jakarta.persistence.EntityManagerFactory#addNamedEntityGraph(String, EntityGraph)
*
* @throws IllegalArgumentException if the graph with the given
* name does not have the given entity type as its root
*
* @since 6.3
*/
<T> RootGraph<T> createEntityGraph(Class<T> rootType, String graphName);
/**
* Retrieve the named {@link EntityGraph} as an immutable graph,
* or return {@code null} if there is no graph with the given

View File

@ -43,6 +43,7 @@ import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.MergeContext;
import org.hibernate.event.spi.PersistContext;
import org.hibernate.event.spi.RefreshContext;
import org.hibernate.graph.RootGraph;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.jdbc.ReturningWork;
import org.hibernate.jdbc.Work;
@ -458,6 +459,11 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
return delegate.createEntityGraph( graphName );
}
@Override
public <T> RootGraph<T> createEntityGraph(Class<T> rootType, String graphName) {
return delegate.createEntityGraph( rootType, graphName );
}
@Override
public RootGraphImplementor<?> getEntityGraph(String graphName) {
return delegate.getEntityGraph( graphName );

View File

@ -530,6 +530,11 @@ public class SessionLazyDelegator implements Session {
return this.lazySession.get().createEntityGraph( graphName );
}
@Override
public <T> RootGraph<T> createEntityGraph(Class<T> rootType, String graphName) {
return this.lazySession.get().createEntityGraph( rootType, graphName );
}
@Override
public RootGraph<?> getEntityGraph(String graphName) {
return this.lazySession.get().getEntityGraph( graphName );

View File

@ -607,6 +607,11 @@ public class SharedSessionDelegatorBaseImpl implements SharedSessionContractImpl
return delegate.createEntityGraph( graphName );
}
@Override
public <T> RootGraph<T> createEntityGraph(Class<T> rootType, String graphName) {
return delegate.createEntityGraph( rootType, graphName );
}
@Override
public RootGraph<?> getEntityGraph(String graphName) {
return delegate.getEntityGraph( graphName );

View File

@ -13,6 +13,7 @@ import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;
import java.util.UUID;
import java.util.function.Function;
@ -42,6 +43,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.transaction.internal.TransactionImpl;
import org.hibernate.engine.transaction.spi.TransactionImplementor;
import org.hibernate.graph.RootGraph;
import org.hibernate.graph.internal.RootGraphImpl;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.id.uuid.StandardRandomStrategy;
@ -52,6 +54,7 @@ import org.hibernate.jpa.spi.NativeQueryConstructorTransformer;
import org.hibernate.jpa.spi.NativeQueryListTransformer;
import org.hibernate.jpa.spi.NativeQueryMapTransformer;
import org.hibernate.jpa.spi.NativeQueryTupleTransformer;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.procedure.internal.ProcedureCallImpl;
@ -1382,6 +1385,22 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return new RootGraphImpl<>( null, getFactory().getJpaMetamodel().entity( rootType ) );
}
@Override @SuppressWarnings("unchecked")
public <T> RootGraph<T> createEntityGraph(Class<T> rootType, String graphName) {
final RootGraph<?> entityGraph = createEntityGraph( graphName );
if ( entityGraph == null ) {
return null;
}
final ManagedDomainType<T> type = getFactory().getJpaMetamodel().managedType( rootType );
final ManagedDomainType<?> graphedType = entityGraph.getGraphedType();
if ( !Objects.equals( graphedType.getTypeName(), type.getTypeName() ) ) {
throw new IllegalArgumentException( "Named entity graph '" + graphName
+ "' is for type '" + graphedType.getTypeName() + "'");
}
return (RootGraph<T>) entityGraph;
}
@Override
public RootGraphImplementor<?> createEntityGraph(String graphName) {
checkOpen();