Skip to content

Commit

Permalink
Small test fixes. Fix unwraps and mutation strategy configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Oct 19, 2021
1 parent 9fa2671 commit a3920e5
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
Expand Up @@ -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;
Expand All @@ -32,6 +33,7 @@
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;
Expand All @@ -42,6 +44,7 @@
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;
Expand Down Expand Up @@ -601,16 +604,42 @@ private SqmMultiTableMutationStrategy resolveSqmMutationStrategy(
return null;
}

//noinspection Convert2Lambda
return strategySelector.resolveDefaultableStrategy(
return strategySelector.resolveStrategy(
SqmMultiTableMutationStrategy.class,
strategyName,
new Callable<SqmMultiTableMutationStrategy>() {
@Override
public SqmMultiTableMutationStrategy call() throws Exception {
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
return (SqmMultiTableMutationStrategy) classLoaderService.classForName( strategyName ).newInstance();
(SqmMultiTableMutationStrategy) null,
strategyClass -> {
Constructor<SqmMultiTableMutationStrategy> dialectConstructor = null;
Constructor<SqmMultiTableMutationStrategy> 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<SqmMultiTableMutationStrategy>) declaredConstructor;
break;
}
else if ( parameterTypes.length == 0 ) {
emptyConstructor = (Constructor<SqmMultiTableMutationStrategy>) 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!" );
}
);
}
Expand Down
Expand Up @@ -23,6 +23,8 @@
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;
Expand Down Expand Up @@ -95,6 +97,7 @@
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;
Expand Down Expand Up @@ -934,6 +937,30 @@ public <T> T unwrap(Class<T> type) {
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() + "'" );
}

Expand Down
Expand Up @@ -2663,6 +2663,9 @@ public <T> T unwrap(Class<T> clazz) {
if ( EntityManager.class.isAssignableFrom( clazz ) ) {
return (T) this;
}
if ( PersistenceContext.class.isAssignableFrom( clazz ) ) {
return (T) this;
}

throw new PersistenceException( "Hibernate cannot unwrap " + clazz );
}
Expand Down
Expand Up @@ -24,7 +24,7 @@ public class DynamicMapTest extends BaseCoreFunctionalTestCase {
@Override
protected String[] getMappings() {
return new String[] {
"/dynamicmap/Test.hbm.xml"
"dynamicmap/Test.hbm.xml"
};
}

Expand Down
Expand Up @@ -202,6 +202,7 @@ public void cleanUpTestData() {
protected boolean isCleanupTestDataRequired() {
return false;
}

@Override
public String[] getMappings() {
return new String[] {
Expand All @@ -219,8 +220,8 @@ public String[] getMappings() {
"/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
Expand Down
Expand Up @@ -46,14 +46,16 @@
* @author Steve Ebersole
*/
public class BulkManipulationTest extends BaseCoreFunctionalTestCase {

@Override
public String[] getMappings() {
return new String[] {
"hql/Animal.hbm.xml",
"hql/Vehicle.hbm.xml",
"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",
Expand Down
Expand Up @@ -45,7 +45,7 @@ protected Class<?>[] getAnnotatedClasses() {
@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" );
Expand Down
Expand Up @@ -75,7 +75,7 @@ public void testJoinedSubclass(SessionFactoryScope scope) {
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 );
Expand Down

0 comments on commit a3920e5

Please sign in to comment.