HHH-10876 - DefaultIdentifierGeneratorFactory does not consider the hibernate.id.new_generator_mappings setting

This commit is contained in:
Vlad Mihalcea 2016-06-22 15:12:30 +03:00
parent 59c0d48f8d
commit eec01edcca
8 changed files with 94 additions and 42 deletions

View File

@ -41,7 +41,6 @@ import org.hibernate.hql.spi.id.IdTableSupportStandardImpl;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
import org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy;
import org.hibernate.hql.spi.id.local.AfterUseAction;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.persister.entity.Lockable;
import org.hibernate.sql.CacheJoinFragment;
@ -466,8 +465,8 @@ public class Cache71Dialect extends Dialect {
}
@Override
public Class getNativeIdentifierGeneratorClass() {
return IdentityGenerator.class;
public String getNativeIdentifierGeneratorStrategy() {
return "identity";
}
// IDENTITY support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -741,7 +741,9 @@ public abstract class Dialect implements ConversionContext {
* Comes into play whenever the user specifies the native generator.
*
* @return The native generator class.
* @deprecated use {@link #getNativeIdentifierGeneratorStrategy()} instead
*/
@Deprecated
public Class getNativeIdentifierGeneratorClass() {
if ( getIdentityColumnSupport().supportsIdentityColumns() ) {
return IdentityGenerator.class;
@ -751,6 +753,22 @@ public abstract class Dialect implements ConversionContext {
}
}
/**
* Resolves the native generation strategy associated to this dialect.
* <p/>
* Comes into play whenever the user specifies the native generator.
*
* @return The native generator strategy.
*/
public String getNativeIdentifierGeneratorStrategy() {
if ( getIdentityColumnSupport().supportsIdentityColumns() ) {
return "identity";
}
else {
return "sequence";
}
}
// IDENTITY support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**

View File

@ -14,7 +14,6 @@ import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.SQL2008StandardLimitHandler;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.MaterializedBlobType;
import org.hibernate.type.WrappedMaterializedBlobType;
@ -62,8 +61,8 @@ public class Oracle12cDialect extends Oracle10gDialect {
}
@Override
public Class getNativeIdentifierGeneratorClass() {
return SequenceStyleGenerator.class;
public String getNativeIdentifierGeneratorStrategy() {
return "sequence";
}
@Override

View File

@ -37,7 +37,6 @@ import org.hibernate.hql.spi.id.IdTableSupportStandardImpl;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
import org.hibernate.hql.spi.id.local.AfterUseAction;
import org.hibernate.hql.spi.id.local.LocalTemporaryTableBulkIdStrategy;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.procedure.internal.PostgresCallableStatementSupport;
import org.hibernate.procedure.spi.CallableStatementSupport;
@ -323,8 +322,8 @@ public class PostgreSQL81Dialect extends Dialect {
}
@Override
public Class getNativeIdentifierGeneratorClass() {
return SequenceStyleGenerator.class;
public String getNativeIdentifierGeneratorStrategy() {
return "sequence";
}
@Override

View File

@ -13,7 +13,10 @@ import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.MappingException;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.id.Assigned;
import org.hibernate.id.Configurable;
@ -124,19 +127,17 @@ public class DefaultIdentifierGeneratorFactory
@Override
public Class getIdentifierGeneratorClass(String strategy) {
if ( "native".equals( strategy ) ) {
return getDialect().getNativeIdentifierGeneratorClass();
}
if ( "hilo".equals( strategy ) ) {
throw new UnsupportedOperationException( "Support for 'hilo' generator has been removed" );
}
String resolvedStrategy = "native".equals( strategy ) ?
getDialect().getNativeIdentifierGeneratorStrategy() : strategy;
Class generatorClass = generatorStrategyToClassNameMap.get( strategy );
Class generatorClass = generatorStrategyToClassNameMap.get( resolvedStrategy );
try {
if ( generatorClass == null ) {
final ClassLoaderService cls = serviceRegistry.getService( ClassLoaderService.class );
generatorClass = cls.classForName( strategy );
generatorClass = cls.classForName( resolvedStrategy );
}
}
catch ( ClassLoadingException e ) {
@ -149,5 +150,16 @@ public class DefaultIdentifierGeneratorFactory
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.serviceRegistry = serviceRegistry;
this.dialect = serviceRegistry.getService( JdbcEnvironment.class ).getDialect();
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
final boolean useNewIdentifierGenerators = configService.getSetting(
AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS,
StandardConverters.BOOLEAN,
true
);
if(!useNewIdentifierGenerators) {
register( "sequence", SequenceGenerator.class );
}
}
}

View File

@ -13,7 +13,9 @@
<class name="Person">
<id name="id" column="id">
<generator class="sequence" />
<generator class="sequence">
<param name="sequence">product_sequence</param>
</generator>
</id>
</class>

View File

@ -6,25 +6,43 @@
*/
package org.hibernate.test.id;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.test.util.jdbc.SQLStatementInterceptor;
import org.junit.Test;
public class SequenceGeneratorTest extends BaseCoreFunctionalTestCase {
import static org.junit.Assert.assertTrue;
public class SequenceGeneratorTest extends BaseNonConfigCoreFunctionalTestCase {
private SQLStatementInterceptor sqlStatementInterceptor;
@Override
protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) {
sqlStatementInterceptor = new SQLStatementInterceptor( sfb );
}
@Override
public String[] getMappings() {
return new String[] { "id/Person.hbm.xml" };
}
@Override
protected void addSettings(Map settings) {
settings.put( Environment.USE_NEW_ID_GENERATOR_MAPPINGS, "false" );
}
/**
* This seems a little trivial, but we need to guarantee that all Dialects start their sequences on a non-0 value.
*/
@ -32,18 +50,23 @@ public class SequenceGeneratorTest extends BaseCoreFunctionalTestCase {
@TestForIssue(jiraKey = "HHH-8814")
@RequiresDialectFeature(DialectChecks.SupportsSequences.class)
@SkipForDialect(
value= SQLServer2012Dialect.class,
comment="SQLServer2012Dialect initializes sequence to minimum value (e.g., Long.MIN_VALUE; Hibernate assumes it is uninitialized."
value = SQLServer2012Dialect.class,
comment = "SQLServer2012Dialect initializes sequence to minimum value (e.g., Long.MIN_VALUE; Hibernate assumes it is uninitialized."
)
public void testStartOfSequence() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
final Person person = new Person();
s.persist(person);
s.persist( person );
tx.commit();
s.close();
assertTrue(person.getId() > 0);
assertTrue( person.getId() > 0 );
assertTrue( sqlStatementInterceptor.getSqlQueries()
.stream()
.filter( sql -> sql.contains( "product_sequence" ) )
.findFirst()
.isPresent() );
}
}

View File

@ -41,7 +41,7 @@ public class CustomSQLTest extends LegacyTestCase {
public static class NonIdentityGeneratorChecker implements DialectCheck {
@Override
public boolean isMatch(Dialect dialect) {
return !PostInsertIdentifierGenerator.class.isAssignableFrom( getDialect().getNativeIdentifierGeneratorClass() );
return !"identity".equals( getDialect().getNativeIdentifierGeneratorStrategy() );
}
}