diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java index c5b7bbb704..04bf153cc6 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java index 55afc817df..2eff91bfaa 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java @@ -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. + *

+ * 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Oracle12cDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Oracle12cDialect.java index 509f84a620..c031ae765e 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Oracle12cDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Oracle12cDialect.java @@ -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 diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java index 562a37c65c..48edd57b70 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java @@ -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 diff --git a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java index e324fdbc81..71c153ae4b 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java @@ -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 ); + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Person.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/id/Person.hbm.xml index cca71c7b6f..30e56432ab 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Person.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/test/id/Person.hbm.xml @@ -13,7 +13,9 @@ - + + product_sequence + diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/SequenceGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/SequenceGeneratorTest.java index 367dcd4871..86e5bd236f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/SequenceGeneratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/id/SequenceGeneratorTest.java @@ -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() ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/legacy/CustomSQLTest.java b/hibernate-core/src/test/java/org/hibernate/test/legacy/CustomSQLTest.java index c67b66ffca..3bd02fc963 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/legacy/CustomSQLTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/legacy/CustomSQLTest.java @@ -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() ); } }