From 248d9cca140614786d9c6f23ec10d0088bb6481c Mon Sep 17 00:00:00 2001 From: Andrej Golovnin Date: Sun, 12 Jun 2016 15:59:35 +0200 Subject: [PATCH] HHH-10837 Add new setting to disable/enable scanning for the static metamodel. --- .../org/hibernate/cfg/AvailableSettings.java | 8 ++++ .../metamodel/internal/MetadataContext.java | 46 ++++++++++++++----- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index c04afb47a3..d2333da20d 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -1491,4 +1491,12 @@ public interface AvailableSettings { * The default behavior is to allow access unless the session is bootstrapped via JPA. */ String ALLOW_JTA_TRANSACTION_ACCESS = "hibernate.jta.allowTransactionAccess"; + + /** + * A setting to control whether Hibernate should scan for a static metamodel + * or not. The default is {@code true}, i.e. Hibernate scans for static metamodel. + * If you don't use a static metamodel in your project, then you can set this setting + * to {@code false} to speed up deployment of your project. + */ + String STATIC_METAMODEL_ENABLED = "hibernate.static_metamodel.enabled"; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java index b0fbb2132a..f9652496f3 100755 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java @@ -22,6 +22,7 @@ import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.Type; import org.hibernate.annotations.common.AssertionFailure; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.EntityManagerMessageLogger; import org.hibernate.internal.HEMLogging; @@ -177,14 +178,23 @@ class MetadataContext { @SuppressWarnings({"unchecked"}) public void wrapUp() { - LOG.trace( "Wrapping up metadata context..." ); + final boolean traceEnabled = LOG.isTraceEnabled(); + if ( traceEnabled ) { + LOG.trace( "Wrapping up metadata context..." ); + } + + boolean staticMetamodelEnabled = Boolean.parseBoolean( + sessionFactory.getProperties().getOrDefault( + AvailableSettings.STATIC_METAMODEL_ENABLED, "true" ).toString() ); //we need to process types from superclasses to subclasses for ( Object mapping : orderedMappings ) { if ( PersistentClass.class.isAssignableFrom( mapping.getClass() ) ) { @SuppressWarnings("unchecked") final PersistentClass safeMapping = (PersistentClass) mapping; - LOG.trace( "Starting entity [" + safeMapping.getEntityName() + "]" ); + if ( traceEnabled ) { + LOG.trace( "Starting entity [" + safeMapping.getEntityName() + ']' ); + } try { final EntityTypeImpl jpa2Mapping = entityTypesByPersistentClass.get( safeMapping ); applyIdMetadata( safeMapping, jpa2Mapping ); @@ -208,16 +218,22 @@ class MetadataContext { } } jpa2Mapping.lock(); - populateStaticMetamodel( jpa2Mapping ); + if ( staticMetamodelEnabled ) { + populateStaticMetamodel( jpa2Mapping ); + } } finally { - LOG.trace( "Completed entity [" + safeMapping.getEntityName() + "]" ); + if ( traceEnabled ) { + LOG.trace( "Completed entity [" + safeMapping.getEntityName() + ']' ); + } } } else if ( MappedSuperclass.class.isAssignableFrom( mapping.getClass() ) ) { @SuppressWarnings("unchecked") final MappedSuperclass safeMapping = (MappedSuperclass) mapping; - LOG.trace( "Starting mapped superclass [" + safeMapping.getMappedClass().getName() + "]" ); + if ( traceEnabled ) { + LOG.trace( "Starting mapped superclass [" + safeMapping.getMappedClass().getName() + ']' ); + } try { final MappedSuperclassTypeImpl jpa2Mapping = mappedSuperclassByMappedSuperclassMapping.get( safeMapping @@ -237,10 +253,14 @@ class MetadataContext { } } jpa2Mapping.lock(); - populateStaticMetamodel( jpa2Mapping ); + if ( staticMetamodelEnabled ) { + populateStaticMetamodel( jpa2Mapping ); + } } finally { - LOG.trace( "Completed mapped superclass [" + safeMapping.getMappedClass().getName() + "]" ); + if ( traceEnabled ) { + LOG.trace( "Completed mapped superclass [" + safeMapping.getMappedClass().getName() + ']' ); + } } } else { @@ -248,8 +268,10 @@ class MetadataContext { } } - for ( EmbeddableTypeImpl embeddable : embeddables.values() ) { - populateStaticMetamodel( embeddable ); + if ( staticMetamodelEnabled ) { + for ( EmbeddableTypeImpl embeddable : embeddables.values() ) { + populateStaticMetamodel( embeddable ); + } } } @@ -331,7 +353,9 @@ class MetadataContext { private Set> buildIdClassAttributes( AbstractIdentifiableType ownerType, Iterator propertyIterator) { - LOG.trace( "Building old-school composite identifier [" + ownerType.getJavaType().getName() + "]" ); + if ( LOG.isTraceEnabled() ) { + LOG.trace( "Building old-school composite identifier [" + ownerType.getJavaType().getName() + ']' ); + } Set> attributes = new HashSet>(); while ( propertyIterator.hasNext() ) { attributes.add( attributeFactory.buildIdAttribute( ownerType, propertyIterator.next() ) ); @@ -345,7 +369,7 @@ class MetadataContext { // should indicate MAP entity mode, skip... return; } - final String metamodelClassName = managedTypeClass.getName() + "_"; + final String metamodelClassName = managedTypeClass.getName() + '_'; try { final Class metamodelClass = Class.forName( metamodelClassName, true, managedTypeClass.getClassLoader() ); // we found the class; so populate it...