From a3920e589245ceeb41a643b94cc0bc501cf44af9 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 7 Oct 2021 08:24:37 +0200 Subject: [PATCH] Small test fixes. Fix unwraps and mutation strategy configuration --- .../SessionFactoryOptionsBuilder.java | 43 ++++++++++++++++--- .../internal/SessionFactoryImpl.java | 27 ++++++++++++ .../org/hibernate/internal/SessionImpl.java | 3 ++ .../test/dynamicmap/DynamicMapTest.java | 2 +- .../test/hql/ASTParserLoadingTest.java | 5 ++- .../test/hql/BulkManipulationTest.java | 4 +- .../InheritanceDeleteBatchTest.java | 2 +- .../joinedsubclass/JoinedSubclassTest.java | 2 +- 8 files changed, 75 insertions(+), 13 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java index 44d30b066b..fd5bb73efb 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java @@ -6,6 +6,7 @@ */ package org.hibernate.boot.internal; +import java.lang.reflect.Constructor; import java.time.ZoneId; import java.util.ArrayList; import java.util.Collections; @@ -32,6 +33,7 @@ import org.hibernate.boot.SchemaAutoTooling; import org.hibernate.boot.TempTableDdlTransactionHandling; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.boot.registry.selector.spi.StrategySelectionException; import org.hibernate.boot.registry.selector.spi.StrategySelector; import org.hibernate.boot.spi.BootstrapContext; import org.hibernate.boot.spi.SessionFactoryOptions; @@ -42,6 +44,7 @@ import org.hibernate.cache.spi.TimestampsCacheFactory; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.BaselineSessionEventsListenerBuilder; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; +import org.hibernate.dialect.Dialect; import org.hibernate.engine.config.internal.ConfigurationServiceImpl; import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; @@ -601,16 +604,42 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { return null; } - //noinspection Convert2Lambda - return strategySelector.resolveDefaultableStrategy( + return strategySelector.resolveStrategy( SqmMultiTableMutationStrategy.class, strategyName, - new Callable() { - @Override - public SqmMultiTableMutationStrategy call() throws Exception { - final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); - return (SqmMultiTableMutationStrategy) classLoaderService.classForName( strategyName ).newInstance(); + (SqmMultiTableMutationStrategy) null, + strategyClass -> { + Constructor dialectConstructor = null; + Constructor emptyConstructor = null; + // todo (6.0) : formalize the allowed constructor parameterizations + for ( Constructor declaredConstructor : strategyClass.getDeclaredConstructors() ) { + final Class[] parameterTypes = declaredConstructor.getParameterTypes(); + if ( parameterTypes.length == 1 && parameterTypes[0] == Dialect.class ) { + dialectConstructor = (Constructor) declaredConstructor; + break; + } + else if ( parameterTypes.length == 0 ) { + emptyConstructor = (Constructor) declaredConstructor; + } } + + try { + if ( dialectConstructor != null ) { + return dialectConstructor.newInstance( + serviceRegistry.getService( JdbcServices.class ).getDialect() + ); + } + else if ( emptyConstructor != null ) { + return emptyConstructor.newInstance(); + } + } + catch (Exception e) { + throw new StrategySelectionException( + String.format( "Could not instantiate named strategy class [%s]", strategyClass.getName() ), + e + ); + } + throw new IllegalArgumentException( "Cannot instantiate the class [" + strategyClass.getName() + "] because it does not have a constructor that accepts a dialect or an empty constructor!" ); } ); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 9c061036a7..e51015de4a 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -23,6 +23,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; import javax.naming.Reference; import javax.naming.StringRefAddr; + +import jakarta.persistence.Cache; import jakarta.persistence.EntityGraph; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.PersistenceContextType; @@ -95,6 +97,7 @@ import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.RootClass; import org.hibernate.metadata.ClassMetadata; import org.hibernate.metadata.CollectionMetadata; +import org.hibernate.metamodel.MappingMetamodel; import org.hibernate.metamodel.RuntimeMetamodels; import org.hibernate.metamodel.internal.RuntimeMetamodelsImpl; import org.hibernate.metamodel.model.domain.AllowableParameterType; @@ -934,6 +937,30 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { return type.cast( this ); } + if ( type.isAssignableFrom( SessionFactoryServiceRegistry.class ) ) { + return type.cast( serviceRegistry ); + } + + if ( type.isAssignableFrom( JdbcServices.class ) ) { + return type.cast( jdbcServices ); + } + + if ( type.isAssignableFrom( Cache.class ) || type.isAssignableFrom( org.hibernate.Cache.class ) ) { + return type.cast( cacheAccess ); + } + + if ( type.isAssignableFrom( JpaMetamodel.class ) ) { + return type.cast( runtimeMetamodels.getJpaMetamodel() ); + } + + if ( type.isAssignableFrom( MetamodelImplementor.class ) || type.isAssignableFrom( MetadataImplementor.class ) ) { + return type.cast( runtimeMetamodels.getMappingMetamodel() ); + } + + if ( type.isAssignableFrom( QueryEngine.class ) ) { + return type.cast( queryEngine ); + } + throw new PersistenceException( "Hibernate cannot unwrap EntityManagerFactory as '" + type.getName() + "'" ); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 972ff59d0d..ea01657106 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -2663,6 +2663,9 @@ public class SessionImpl if ( EntityManager.class.isAssignableFrom( clazz ) ) { return (T) this; } + if ( PersistenceContext.class.isAssignableFrom( clazz ) ) { + return (T) this; + } throw new PersistenceException( "Hibernate cannot unwrap " + clazz ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicmap/DynamicMapTest.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicmap/DynamicMapTest.java index f503048fc3..0e73b3f4fb 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicmap/DynamicMapTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/dynamicmap/DynamicMapTest.java @@ -24,7 +24,7 @@ public class DynamicMapTest extends BaseCoreFunctionalTestCase { @Override protected String[] getMappings() { return new String[] { - "/dynamicmap/Test.hbm.xml" + "dynamicmap/Test.hbm.xml" }; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java index c9f40bd750..3d55a2525a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java @@ -202,6 +202,7 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase { protected boolean isCleanupTestDataRequired() { return false; } + @Override public String[] getMappings() { return new String[] { @@ -219,8 +220,8 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase { "/org/hibernate/orm/test/cid/LineItem.hbm.xml", "/org/hibernate/orm/test/cid/Product.hbm.xml", "/org/hibernate/orm/test/any/hbm/Properties.hbm.xml", - "legacy/Commento.hbm.xml", - "legacy/Marelo.hbm.xml" + "/org/hibernate/orm/test/legacy/Commento.hbm.xml", + "/org/hibernate/orm/test/legacy/Marelo.hbm.xml" }; } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/BulkManipulationTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/BulkManipulationTest.java index be6f02f54f..c9cbfcc8bd 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/BulkManipulationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/BulkManipulationTest.java @@ -46,6 +46,8 @@ import static org.junit.Assert.fail; * @author Steve Ebersole */ public class BulkManipulationTest extends BaseCoreFunctionalTestCase { + + @Override public String[] getMappings() { return new String[] { "hql/Animal.hbm.xml", @@ -53,7 +55,7 @@ public class BulkManipulationTest extends BaseCoreFunctionalTestCase { "hql/KeyManyToOneEntity.hbm.xml", "hql/Versions.hbm.xml", "hql/FooBarCopy.hbm.xml", - "legacy/Multi.hbm.xml", + "/org/hibernate/orm/test/legacy/Multi.hbm.xml", "hql/EntityWithCrazyCompositeKey.hbm.xml", "hql/SimpleEntityWithAssociation.hbm.xml", "hql/BooleanLiteralEntity.hbm.xml", diff --git a/hibernate-core/src/test/java/org/hibernate/test/inheritance/InheritanceDeleteBatchTest.java b/hibernate-core/src/test/java/org/hibernate/test/inheritance/InheritanceDeleteBatchTest.java index ca35a519c2..945d2b36b2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/inheritance/InheritanceDeleteBatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/inheritance/InheritanceDeleteBatchTest.java @@ -45,7 +45,7 @@ public class InheritanceDeleteBatchTest extends BaseCoreFunctionalTestCase { @Override protected void configure(Configuration configuration) { configuration.setProperty( - AvailableSettings.HQL_BULK_ID_STRATEGY, + AvailableSettings.QUERY_MULTI_TABLE_MUTATION_STRATEGY, InlineStrategy.class.getName() ); configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, "true" ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java index 4bc5538171..ca06b1cdb7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java @@ -75,7 +75,7 @@ public class JoinedSubclassTest { s.save( mark ); s.save( joe ); - assertEquals( s.createQuery( "from java.io.Serializable" ).list().size(), 0 ); + assertEquals( s.createQuery( "from java.lang.Object" ).list().size(), 0 ); assertEquals( s.createQuery( "from Person" ).list().size(), 3 ); assertEquals( s.createQuery( "from Person p where p.class = Customer" ).list().size(), 1 );