HHH-10876 - DefaultIdentifierGeneratorFactory does not consider the hibernate.id.new_generator_mappings setting
(cherry picked from commiteec01edcca
) Conflicts: hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java (cherry picked from commitd4b81560bf
) Conflicts: hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java
This commit is contained in:
parent
e6fe44f973
commit
a505028533
|
@ -35,7 +35,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;
|
||||
|
@ -472,8 +471,8 @@ public class Cache71Dialect extends Dialect {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Class getNativeIdentifierGeneratorClass() {
|
||||
return IdentityGenerator.class;
|
||||
public String getNativeIdentifierGeneratorStrategy() {
|
||||
return "identity";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -728,7 +728,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;
|
||||
|
@ -738,6 +740,21 @@ 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
@ -67,8 +66,8 @@ public class Oracle12cDialect extends Oracle10gDialect {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Class getNativeIdentifierGeneratorClass() {
|
||||
return SequenceStyleGenerator.class;
|
||||
public String getNativeIdentifierGeneratorStrategy() {
|
||||
return "sequence";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -28,8 +28,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.SequenceGenerator;
|
||||
import org.hibernate.id.enhanced.SequenceStyleGenerator;
|
||||
import org.hibernate.internal.util.JdbcExceptionHelper;
|
||||
import org.hibernate.procedure.internal.PostgresCallableStatementSupport;
|
||||
import org.hibernate.procedure.spi.CallableStatementSupport;
|
||||
|
@ -328,8 +326,8 @@ public class PostgreSQL81Dialect extends Dialect {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Class getNativeIdentifierGeneratorClass() {
|
||||
return SequenceStyleGenerator.class;
|
||||
public String getNativeIdentifierGeneratorStrategy() {
|
||||
return "sequence";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -6,44 +6,67 @@
|
|||
*/
|
||||
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;
|
||||
|
||||
@Override
|
||||
public String[] getMappings() {
|
||||
return new String[] { "id/Person.hbm.xml" };
|
||||
}
|
||||
public class SequenceGeneratorTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
/**
|
||||
* This seems a little trivial, but we need to guarantee that all Dialects start their sequences on a non-0 value.
|
||||
*/
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-8814")
|
||||
@RequiresDialectFeature(DialectChecks.SupportsSequences.class)
|
||||
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.
|
||||
*/
|
||||
@Test
|
||||
@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);
|
||||
tx.commit();
|
||||
s.close();
|
||||
|
||||
assertTrue(person.getId() > 0);
|
||||
}
|
||||
public void testStartOfSequence() throws Exception {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
final Person person = new Person();
|
||||
s.persist( person );
|
||||
tx.commit();
|
||||
s.close();
|
||||
|
||||
assertTrue( person.getId() > 0 );
|
||||
assertTrue( sqlStatementInterceptor.getSqlQueries()
|
||||
.stream()
|
||||
.filter( sql -> sql.contains( "product_sequence" ) )
|
||||
.findFirst()
|
||||
.isPresent() );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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() );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue