mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-07 19:58:16 +00:00
HHH-10876 - DefaultIdentifierGeneratorFactory does not consider the hibernate.id.new_generator_mappings setting
(cherry picked from commit eec01edcca9c01f3a748bdc3d203e7818447e312) Conflicts: hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java (cherry picked from commit d4b81560bf06ffff5cef4429ea7887bdd40dff98) 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.MultiTableBulkIdStrategy;
|
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
|
||||||
import org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy;
|
import org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy;
|
||||||
import org.hibernate.hql.spi.id.local.AfterUseAction;
|
import org.hibernate.hql.spi.id.local.AfterUseAction;
|
||||||
import org.hibernate.id.IdentityGenerator;
|
|
||||||
import org.hibernate.internal.util.StringHelper;
|
import org.hibernate.internal.util.StringHelper;
|
||||||
import org.hibernate.persister.entity.Lockable;
|
import org.hibernate.persister.entity.Lockable;
|
||||||
import org.hibernate.sql.CacheJoinFragment;
|
import org.hibernate.sql.CacheJoinFragment;
|
||||||
@ -472,8 +471,8 @@ public boolean supportsIdentityColumns() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class getNativeIdentifierGeneratorClass() {
|
public String getNativeIdentifierGeneratorStrategy() {
|
||||||
return IdentityGenerator.class;
|
return "identity";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -728,7 +728,9 @@ public final Map<String, SQLFunction> getFunctions() {
|
|||||||
* Comes into play whenever the user specifies the native generator.
|
* Comes into play whenever the user specifies the native generator.
|
||||||
*
|
*
|
||||||
* @return The native generator class.
|
* @return The native generator class.
|
||||||
|
* @deprecated use {@link #getNativeIdentifierGeneratorStrategy()} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public Class getNativeIdentifierGeneratorClass() {
|
public Class getNativeIdentifierGeneratorClass() {
|
||||||
if ( getIdentityColumnSupport().supportsIdentityColumns() ) {
|
if ( getIdentityColumnSupport().supportsIdentityColumns() ) {
|
||||||
return IdentityGenerator.class;
|
return IdentityGenerator.class;
|
||||||
@ -738,6 +740,21 @@ public Class getNativeIdentifierGeneratorClass() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// IDENTITY support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
import org.hibernate.dialect.pagination.SQL2008StandardLimitHandler;
|
import org.hibernate.dialect.pagination.SQL2008StandardLimitHandler;
|
||||||
import org.hibernate.engine.config.spi.ConfigurationService;
|
import org.hibernate.engine.config.spi.ConfigurationService;
|
||||||
import org.hibernate.engine.config.spi.StandardConverters;
|
import org.hibernate.engine.config.spi.StandardConverters;
|
||||||
import org.hibernate.id.enhanced.SequenceStyleGenerator;
|
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
import org.hibernate.type.MaterializedBlobType;
|
import org.hibernate.type.MaterializedBlobType;
|
||||||
import org.hibernate.type.WrappedMaterializedBlobType;
|
import org.hibernate.type.WrappedMaterializedBlobType;
|
||||||
@ -67,8 +66,8 @@ public boolean supportsInsertSelectIdentity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class getNativeIdentifierGeneratorClass() {
|
public String getNativeIdentifierGeneratorStrategy() {
|
||||||
return SequenceStyleGenerator.class;
|
return "sequence";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -28,8 +28,6 @@
|
|||||||
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
|
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
|
||||||
import org.hibernate.hql.spi.id.local.AfterUseAction;
|
import org.hibernate.hql.spi.id.local.AfterUseAction;
|
||||||
import org.hibernate.hql.spi.id.local.LocalTemporaryTableBulkIdStrategy;
|
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.internal.util.JdbcExceptionHelper;
|
||||||
import org.hibernate.procedure.internal.PostgresCallableStatementSupport;
|
import org.hibernate.procedure.internal.PostgresCallableStatementSupport;
|
||||||
import org.hibernate.procedure.spi.CallableStatementSupport;
|
import org.hibernate.procedure.spi.CallableStatementSupport;
|
||||||
@ -328,8 +326,8 @@ public boolean supportsCaseInsensitiveLike() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class getNativeIdentifierGeneratorClass() {
|
public String getNativeIdentifierGeneratorStrategy() {
|
||||||
return SequenceStyleGenerator.class;
|
return "sequence";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -13,7 +13,10 @@
|
|||||||
import org.hibernate.MappingException;
|
import org.hibernate.MappingException;
|
||||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||||
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
|
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
|
||||||
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.dialect.Dialect;
|
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.engine.jdbc.env.spi.JdbcEnvironment;
|
||||||
import org.hibernate.id.Assigned;
|
import org.hibernate.id.Assigned;
|
||||||
import org.hibernate.id.Configurable;
|
import org.hibernate.id.Configurable;
|
||||||
@ -124,19 +127,17 @@ public IdentifierGenerator createIdentifierGenerator(String strategy, Type type,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class getIdentifierGeneratorClass(String strategy) {
|
public Class getIdentifierGeneratorClass(String strategy) {
|
||||||
if ( "native".equals( strategy ) ) {
|
|
||||||
return getDialect().getNativeIdentifierGeneratorClass();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( "hilo".equals( strategy ) ) {
|
if ( "hilo".equals( strategy ) ) {
|
||||||
throw new UnsupportedOperationException( "Support for 'hilo' generator has been removed" );
|
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 {
|
try {
|
||||||
if ( generatorClass == null ) {
|
if ( generatorClass == null ) {
|
||||||
final ClassLoaderService cls = serviceRegistry.getService( ClassLoaderService.class );
|
final ClassLoaderService cls = serviceRegistry.getService( ClassLoaderService.class );
|
||||||
generatorClass = cls.classForName( strategy );
|
generatorClass = cls.classForName( resolvedStrategy );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch ( ClassLoadingException e ) {
|
catch ( ClassLoadingException e ) {
|
||||||
@ -149,5 +150,16 @@ public Class getIdentifierGeneratorClass(String strategy) {
|
|||||||
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
|
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
|
||||||
this.serviceRegistry = serviceRegistry;
|
this.serviceRegistry = serviceRegistry;
|
||||||
this.dialect = serviceRegistry.getService( JdbcEnvironment.class ).getDialect();
|
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">
|
<class name="Person">
|
||||||
<id name="id" column="id">
|
<id name="id" column="id">
|
||||||
<generator class="sequence" />
|
<generator class="sequence">
|
||||||
|
<param name="sequence">product_sequence</param>
|
||||||
|
</generator>
|
||||||
</id>
|
</id>
|
||||||
</class>
|
</class>
|
||||||
|
|
||||||
|
@ -6,44 +6,67 @@
|
|||||||
*/
|
*/
|
||||||
package org.hibernate.test.id;
|
package org.hibernate.test.id;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
|
import org.hibernate.boot.SessionFactoryBuilder;
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.dialect.SQLServer2012Dialect;
|
import org.hibernate.dialect.SQLServer2012Dialect;
|
||||||
|
|
||||||
import org.hibernate.testing.DialectChecks;
|
import org.hibernate.testing.DialectChecks;
|
||||||
import org.hibernate.testing.RequiresDialectFeature;
|
import org.hibernate.testing.RequiresDialectFeature;
|
||||||
import org.hibernate.testing.SkipForDialect;
|
import org.hibernate.testing.SkipForDialect;
|
||||||
import org.hibernate.testing.TestForIssue;
|
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;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class SequenceGeneratorTest extends BaseCoreFunctionalTestCase {
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@Override
|
public class SequenceGeneratorTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
public String[] getMappings() {
|
|
||||||
return new String[] { "id/Person.hbm.xml" };
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private SQLStatementInterceptor sqlStatementInterceptor;
|
||||||
* This seems a little trivial, but we need to guarantee that all Dialects start their sequences on a non-0 value.
|
|
||||||
*/
|
@Override
|
||||||
@Test
|
protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) {
|
||||||
@TestForIssue(jiraKey = "HHH-8814")
|
sqlStatementInterceptor = new SQLStatementInterceptor( sfb );
|
||||||
@RequiresDialectFeature(DialectChecks.SupportsSequences.class)
|
}
|
||||||
|
|
||||||
|
@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(
|
@SkipForDialect(
|
||||||
value= SQLServer2012Dialect.class,
|
value = SQLServer2012Dialect.class,
|
||||||
comment="SQLServer2012Dialect initializes sequence to minimum value (e.g., Long.MIN_VALUE; Hibernate assumes it is uninitialized."
|
comment = "SQLServer2012Dialect initializes sequence to minimum value (e.g., Long.MIN_VALUE; Hibernate assumes it is uninitialized."
|
||||||
)
|
)
|
||||||
public void testStartOfSequence() throws Exception {
|
public void testStartOfSequence() throws Exception {
|
||||||
Session s = openSession();
|
Session s = openSession();
|
||||||
Transaction tx = s.beginTransaction();
|
Transaction tx = s.beginTransaction();
|
||||||
final Person person = new Person();
|
final Person person = new Person();
|
||||||
s.persist(person);
|
s.persist( person );
|
||||||
tx.commit();
|
tx.commit();
|
||||||
s.close();
|
s.close();
|
||||||
|
|
||||||
assertTrue(person.getId() > 0);
|
assertTrue( person.getId() > 0 );
|
||||||
}
|
assertTrue( sqlStatementInterceptor.getSqlQueries()
|
||||||
|
.stream()
|
||||||
|
.filter( sql -> sql.contains( "product_sequence" ) )
|
||||||
|
.findFirst()
|
||||||
|
.isPresent() );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ public String[] getMappings() {
|
|||||||
public static class NonIdentityGeneratorChecker implements DialectCheck {
|
public static class NonIdentityGeneratorChecker implements DialectCheck {
|
||||||
@Override
|
@Override
|
||||||
public boolean isMatch(Dialect dialect) {
|
public boolean isMatch(Dialect dialect) {
|
||||||
return !PostInsertIdentifierGenerator.class.isAssignableFrom( getDialect().getNativeIdentifierGeneratorClass() );
|
return !"identity".equals( getDialect().getNativeIdentifierGeneratorStrategy() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user