6 - SQM based on JPA type system, RuntimeModelCreationProcess

This commit is contained in:
Andrea Boriero 2019-06-01 14:05:33 +01:00
parent f20d36be40
commit 52aff8055e
7 changed files with 64 additions and 66 deletions

View File

@ -73,11 +73,8 @@ import org.hibernate.engine.jndi.spi.JndiService;
import org.hibernate.engine.profile.Association;
import org.hibernate.engine.profile.Fetch;
import org.hibernate.engine.profile.FetchProfile;
import org.hibernate.query.hql.spi.NamedHqlQueryMemento;
import org.hibernate.query.spi.QueryPlanCache;
import org.hibernate.engine.query.spi.ReturnMetadata;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.query.hql.internal.NamedHqlQueryMementoImpl;
import org.hibernate.engine.spi.SessionBuilderImplementor;
import org.hibernate.engine.spi.SessionEventListenerManager;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -100,13 +97,15 @@ import org.hibernate.jpa.internal.PersistenceUnitUtilImpl;
import org.hibernate.mapping.RootClass;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metadata.CollectionMetadata;
import org.hibernate.metamodel.model.domain.internal.DomainMetamodelImpl;
import org.hibernate.metamodel.spi.MetamodelImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.Loadable;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.proxy.HibernateProxyHelper;
import org.hibernate.query.hql.internal.NamedHqlQueryMementoImpl;
import org.hibernate.query.hql.spi.NamedHqlQueryMemento;
import org.hibernate.query.spi.QueryPlanCache;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder;
import org.hibernate.query.NativeQuery;
@ -299,11 +298,8 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
LOG.debug( "Instantiated session factory" );
this.metamodel = metadata.getTypeConfiguration().scope( this );
( (DomainMetamodelImpl) this.metamodel ).initialize(
metadata,
determineJpaMetaModelPopulationSetting( properties )
);
this.metamodel = (MetamodelImplementor) metadata.getTypeConfiguration().scope( this )
.create( metadata, determineJpaMetaModelPopulationSetting( properties ) );
//Named Queries:
this.namedQueryRepository = metadata.buildNamedQueryRepository( this );

View File

@ -12,8 +12,6 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cache.cfg.internal.DomainDataRegionConfigImpl;
import org.hibernate.cache.cfg.spi.DomainDataRegionConfig;
@ -25,9 +23,11 @@ import org.hibernate.mapping.RootClass;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.internal.DomainMetamodelImpl;
import org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl;
import org.hibernate.metamodel.spi.DomainMetamodel;
import org.hibernate.metamodel.spi.MetamodelImplementor;
import org.hibernate.persister.spi.PersisterCreationContext;
import org.hibernate.persister.spi.PersisterFactory;
import org.hibernate.type.spi.TypeConfiguration;
import static org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting.determineJpaMetaModelPopulationSetting;
@ -83,48 +83,23 @@ public class RuntimeModelCreationProcess {
// ````
private final SessionFactoryImplementor sessionFactory;
private TypeConfiguration typeConfiguration;
public RuntimeModelCreationProcess(
SessionFactoryImplementor sessionFactory,
TypeConfiguration typeConfiguration) {
this.sessionFactory = sessionFactory;
this.typeConfiguration = typeConfiguration;
}
/**
* Perform the runtime metamodel creation based on the information obtained during
* the first phase of booting, returning the
*/
public static MetamodelImplementor execute(
SessionFactoryImplementor sessionFactory,
BootstrapContext bootstrapContext,
MetadataBuildingContext metadataBuildingContext,
public DomainMetamodel create(
MetadataImplementor bootMetamodel,
JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting) {
return new RuntimeModelCreationProcess(
sessionFactory,
bootstrapContext,
metadataBuildingContext,
bootMetamodel,
jpaMetaModelPopulationSetting
).execute();
}
private final SessionFactoryImplementor sessionFactory;
private final BootstrapContext bootstrapContext;
private final MetadataBuildingContext metadataBuildingContext;
private final MetadataImplementor bootMetamodel;
private final JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting;
private JpaMetamodel jpaMetamodel;
private MetamodelImplementor domainMetamodel;
public RuntimeModelCreationProcess(
SessionFactoryImplementor sessionFactory,
BootstrapContext bootstrapContext,
MetadataBuildingContext metadataBuildingContext,
MetadataImplementor bootMetamodel,
JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting) {
this.sessionFactory = sessionFactory;
this.bootstrapContext = bootstrapContext;
this.metadataBuildingContext = metadataBuildingContext;
this.bootMetamodel = bootMetamodel;
this.jpaMetaModelPopulationSetting = jpaMetaModelPopulationSetting;
}
public MetamodelImplementor execute() {
final PersisterCreationContext persisterCreationContext = new PersisterCreationContext() {
@Override
public SessionFactoryImplementor getSessionFactory() {
@ -139,8 +114,7 @@ public class RuntimeModelCreationProcess {
final PersisterFactory persisterFactory = sessionFactory.getServiceRegistry()
.getService( PersisterFactory.class );
final InflightRuntimeMetamodel inflightRuntimeMetamodel = new InflightRuntimeMetamodel( bootstrapContext.getTypeConfiguration() );
final InflightRuntimeMetamodel inflightRuntimeMetamodel = new InflightRuntimeMetamodel( typeConfiguration );
primeSecondLevelCacheRegions( bootMetamodel );
@ -156,12 +130,16 @@ public class RuntimeModelCreationProcess {
);
this.jpaMetamodel = new JpaMetamodelImpl(
JpaMetamodel jpaMetamodel = new JpaMetamodelImpl(
inflightRuntimeMetamodel,
bootMetamodel.getNamedEntityGraphs().values()
);
this.domainMetamodel = new DomainMetamodelImpl( sessionFactory, inflightRuntimeMetamodel, this.jpaMetamodel );
DomainMetamodelImpl domainMetamodel = new DomainMetamodelImpl(
sessionFactory,
inflightRuntimeMetamodel,
jpaMetamodel
);
return domainMetamodel;
}

View File

@ -77,7 +77,6 @@ public interface JpaMetamodel extends javax.persistence.metamodel.Metamodel {
*/
<X> EntityDomainType<X> findEntityType(Class<X> cls);
/
void visitRootEntityTypes(Consumer<EntityDomainType<?>> action);
void visitEmbeddables(Consumer<EmbeddableDomainType<?>> action);

View File

@ -64,11 +64,7 @@ public class DomainMetamodelImpl implements DomainMetamodel, MetamodelImplemento
private final JpaMetamodel jpaMetamodel;
// private final Map<Class<?>, EntityDomainType<?>> jpaEntityTypeMap = new ConcurrentHashMap<>();
// private final Map<String, EntityDomainType<?>> jpaEntityTypesByEntityName = new ConcurrentHashMap<>();
// private final Map<Class<?>, MappedSuperclassType<?>> jpaMappedSuperclassTypeMap = new ConcurrentHashMap<>();
// private final Set<EmbeddableDomainType<?>> jpaEmbeddableTypes = new CopyOnWriteArraySet<>();
private final Map<Class, String> entityProxyInterfaceMap = new ConcurrentHashMap<>();
private final Map<Class, String> entityProxyInterfaceMap;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -132,7 +128,7 @@ public class DomainMetamodelImpl implements DomainMetamodel, MetamodelImplemento
this.collectionPersisterMap = runtimeMetamodel.getCollectionPersisterMap();
this.collectionRolesByEntityParticipant = runtimeMetamodel.getCollectionRolesByEntityParticipant();
this.entityNameResolvers = runtimeMetamodel.getEntityNameResolvers();
this.entityProxyInterfaceMap = runtimeMetamodel.getEntityProxyInterfaceMap();
}
@Override

View File

@ -125,7 +125,24 @@ public class JpaMetamodelImpl implements JpaMetamodel {
public void visitManagedTypes(Consumer<ManagedDomainType<?>> action) {
visitEntityTypes( (Consumer) action );
visitEmbeddables( (Consumer) action );
mappedSuperclassTypeMap.values().forEach( (Consumer) action );
mappedSuperclassTypeMap.values().forEach( action );
}
@Override
public <X> ManagedDomainType<X> findManagedType(Class<X> cls) {
ManagedType<?> type = entityDescriptorMap.get( cls );
if ( type == null ) {
type = mappedSuperclassTypeMap.get( cls );
}
if ( type == null ) {
type = embeddableDescriptorMap.get( cls );
}
if ( type == null ) {
return null;
}
//noinspection unchecked
return (ManagedDomainType<X>) type;
}
@Override
@ -133,6 +150,16 @@ public class JpaMetamodelImpl implements JpaMetamodel {
entityDescriptorMap.values().forEach( action );
}
@Override
public <X> EntityDomainType<X> findEntityType(Class<X> cls) {
final EntityType<?> entityType = entityDescriptorMap.get( cls );
if ( entityType == null ) {
return null;
}
//noinspection unchecked
return (EntityDomainType<X>) entityType;
}
@Override
public void visitRootEntityTypes(Consumer<EntityDomainType<?>> action) {
entityDescriptorMap.values().forEach(

View File

@ -31,6 +31,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.id.uuid.LocalObjectUuidHelper;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.SessionFactoryRegistry;
import org.hibernate.metamodel.internal.RuntimeModelCreationProcess;
import org.hibernate.metamodel.model.domain.internal.DomainMetamodelImpl;
import org.hibernate.metamodel.spi.MetamodelImplementor;
import org.hibernate.query.BinaryArithmeticOperator;
@ -171,7 +172,7 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable {
scope.setMetadataBuildingContext( metadataBuildingContext );
}
public MetamodelImplementor scope(SessionFactoryImplementor sessionFactory) {
public RuntimeModelCreationProcess scope(SessionFactoryImplementor sessionFactory) {
log.debugf( "Scoping TypeConfiguration [%s] to SessionFactoryImpl [%s]", this, sessionFactory );
for ( Map.Entry<String, String> importEntry : scope.metadataBuildingContext.getMetadataCollector().getImports().entrySet() ) {
@ -184,7 +185,7 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable {
scope.setSessionFactory( sessionFactory );
sessionFactory.addObserver( this );
return new DomainMetamodelImpl( sessionFactory, this, this.jpaMetamodel );
return new RuntimeModelCreationProcess( sessionFactory, this );
}
/**

View File

@ -29,7 +29,7 @@ import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting;
import org.hibernate.metamodel.model.domain.internal.DomainMetamodelImpl;
import org.hibernate.metamodel.internal.RuntimeModelCreationProcess;
import org.junit.Test;
@ -94,10 +94,11 @@ public class MetadataTest extends BaseEntityManagerFunctionalTestCase {
.addAnnotatedClass( WithGenericCollection.class )
.buildMetadata();
SessionFactoryImplementor sfi = (SessionFactoryImplementor) metadata.buildSessionFactory();
DomainMetamodelImpl metamodel = new DomainMetamodelImpl( sfi, ( (MetadataImplementor) metadata ).getTypeConfiguration(),
this.jpaMetamodel
);
metamodel.initialize( (MetadataImplementor) metadata, JpaMetaModelPopulationSetting.IGNORE_UNSUPPORTED );
new RuntimeModelCreationProcess(
sfi,
( (MetadataImplementor) metadata ).getTypeConfiguration()
).create( (MetadataImplementor) metadata, JpaMetaModelPopulationSetting.IGNORE_UNSUPPORTED );
sfi.close();
}