Skip to content

Commit

Permalink
HHH-12973 - Inconsistent identity generation when using the default @…
Browse files Browse the repository at this point in the history
…SequenceGenerator with a database sequence having the increment size of 1
  • Loading branch information
vladmihalcea committed Oct 8, 2018
1 parent ec21c6d commit 1db476d
Show file tree
Hide file tree
Showing 58 changed files with 2,246 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1961,4 +1961,19 @@ public interface AvailableSettings extends org.hibernate.jpa.AvailableSettings {
*/
String QUERY_STATISTICS_MAX_SIZE = "hibernate.statistics.query_max_size";

/**
* This setting defines the {@link org.hibernate.id.SequenceMismatchStrategy} used when
* Hibernate detects a mismatch between a sequence configuration in an entity mapping
* and its database sequence object counterpart.
* </p>
* Possible values are {@link org.hibernate.id.SequenceMismatchStrategy#EXCEPTION},
* {@link org.hibernate.id.SequenceMismatchStrategy#LOG}, and
* {@link org.hibernate.id.SequenceMismatchStrategy#FIX}.
* </p>
* The default value is given by the {@link org.hibernate.id.SequenceMismatchStrategy#EXCEPTION},
* meaning that an Exception is thrown when detecting such a conflict.
*
* @since 5.4
*/
String SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY = "hibernate.id.sequence.increment_size_mismatch_strategy";
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
import org.hibernate.procedure.internal.StandardCallableStatementSupport;
import org.hibernate.procedure.spi.CallableStatementSupport;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorHANADatabaseImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.tool.schema.internal.StandardTableExporter;
import org.hibernate.tool.schema.spi.Exporter;
import org.hibernate.type.StandardBasicTypes;
Expand Down Expand Up @@ -1108,7 +1110,12 @@ public String getNotExpression(final String expression) {

@Override
public String getQuerySequencesString() {
return "select sequence_name from sys.sequences";
return "select * from sys.sequences";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
return SequenceInformationExtractorHANADatabaseImpl.INSTANCE;
}

@Override
Expand Down Expand Up @@ -1636,5 +1643,4 @@ public int registerResultSetOutParameter(CallableStatement statement, String nam
public boolean supportsNoWait() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.hibernate.dialect.identity.IdentityColumnSupport;
import org.hibernate.dialect.pagination.CUBRIDLimitHandler;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorCUBRIDDatabaseImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

/**
Expand Down Expand Up @@ -284,7 +286,12 @@ public boolean supportsExistsInSelect() {

@Override
public String getQuerySequencesString() {
return "select name from db_serial";
return "select * from db_serial";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
return SequenceInformationExtractorCUBRIDDatabaseImpl.INSTANCE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public String getDropSequenceString(String sequenceName) {
}

public String getQuerySequencesString() {
return "select name from sysibm.syssequences";
return "select * from sysibm.syssequences";
}
}
16 changes: 14 additions & 2 deletions hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
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.hql.spi.id.local.LocalTemporaryTableBulkIdStrategy;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorDB2DatabaseImpl;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.descriptor.sql.DecimalTypeDescriptor;
import org.hibernate.type.descriptor.sql.SmallIntTypeDescriptor;
Expand Down Expand Up @@ -267,7 +269,17 @@ public boolean supportsPooledSequences() {

@Override
public String getQuerySequencesString() {
return "select seqname from sysibm.syssequences";
return "select * from syscat.sequences";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
if ( getQuerySequencesString() == null ) {
return SequenceInformationExtractorNoOpImpl.INSTANCE;
}
else {
return SequenceInformationExtractorDB2DatabaseImpl.INSTANCE;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.sql.CaseFragment;
import org.hibernate.sql.DerbyCaseFragment;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorDerbyDatabaseImpl;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;

import org.jboss.logging.Logger;

Expand Down Expand Up @@ -130,13 +133,23 @@ public boolean supportsSequences() {
@Override
public String getQuerySequencesString() {
if ( supportsSequences() ) {
return "select SEQUENCENAME from SYS.SYSSEQUENCES";
return "select sys.sysschemas.schemaname as sequence_schema, sys.syssequences.* from sys.syssequences left join sys.sysschemas on sys.syssequences.schemaid = sys.sysschemas.schemaid";
}
else {
return null;
}
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
if ( getQuerySequencesString() == null ) {
return SequenceInformationExtractorNoOpImpl.INSTANCE;
}
else {
return SequenceInformationExtractorDerbyDatabaseImpl.INSTANCE;
}
}

@Override
public String getSequenceNextValString(String sequenceName) {
if ( supportsSequences() ) {
Expand Down
23 changes: 13 additions & 10 deletions hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorH2DatabaseImpl;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

Expand Down Expand Up @@ -81,18 +81,15 @@ public boolean bindLimitParametersInReverseOrder() {
public H2Dialect() {
super();

String querySequenceString = "select sequence_name from information_schema.sequences";
SequenceInformationExtractor sequenceInformationExtractor = SequenceInformationExtractorH2DatabaseImpl.INSTANCE;
int buildId = Integer.MIN_VALUE;

try {
// HHH-2300
final Class h2ConstantsClass = ReflectHelper.classForName( "org.h2.engine.Constants" );
final int majorVersion = (Integer) h2ConstantsClass.getDeclaredField( "VERSION_MAJOR" ).get( null );
final int minorVersion = (Integer) h2ConstantsClass.getDeclaredField( "VERSION_MINOR" ).get( null );
final int buildId = (Integer) h2ConstantsClass.getDeclaredField( "BUILD_ID" ).get( null );
if ( buildId < 32 ) {
querySequenceString = "select name from information_schema.sequences";
sequenceInformationExtractor = SequenceInformationExtractorLegacyImpl.INSTANCE;
}
buildId = (Integer) h2ConstantsClass.getDeclaredField( "BUILD_ID" ).get( null );

if ( ! ( majorVersion > 1 || minorVersion > 2 || buildId >= 139 ) ) {
LOG.unsupportedMultiTableBulkHqlJpaql( majorVersion, minorVersion, buildId );
}
Expand All @@ -103,8 +100,14 @@ public H2Dialect() {
LOG.undeterminedH2Version();
}

this.querySequenceString = querySequenceString;
this.sequenceInformationExtractor = sequenceInformationExtractor;
if ( buildId >= 32 ) {
this.sequenceInformationExtractor = SequenceInformationExtractorH2DatabaseImpl.INSTANCE;
this.querySequenceString = "select * from information_schema.sequences";
}
else {
this.sequenceInformationExtractor = SequenceInformationExtractorNoOpImpl.INSTANCE;
this.querySequenceString = null;
}

registerColumnType( Types.BOOLEAN, "boolean" );
registerColumnType( Types.BIGINT, "bigint" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.dialect;

import java.io.Serializable;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;
Expand Down Expand Up @@ -48,10 +49,12 @@
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.persister.entity.Lockable;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorHANADatabaseImpl;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorHSQLDBDatabaseImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

import org.jboss.logging.Logger;
import java.sql.DatabaseMetaData;

/**
* An SQL dialect compatible with HSQLDB (HyperSQL).
Expand Down Expand Up @@ -369,7 +372,12 @@ public String getSequenceNextValString(String sequenceName) {
@Override
public String getQuerySequencesString() {
// this assumes schema support, which is present in 1.8.0 and later...
return "select sequence_name from information_schema.system_sequences";
return "select * from information_schema.sequences";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
return SequenceInformationExtractorHSQLDBDatabaseImpl.INSTANCE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.hibernate.hql.spi.id.local.AfterUseAction;
import org.hibernate.hql.spi.id.local.LocalTemporaryTableBulkIdStrategy;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorInformixDatabaseImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

/**
Expand Down Expand Up @@ -148,7 +150,7 @@ public String getCreateSequenceString(String sequenceName) {

@Override
public String getDropSequenceString(String sequenceName) {
return "drop sequence " + sequenceName + " restrict";
return "drop sequence " + sequenceName;
}

@Override
Expand All @@ -173,7 +175,12 @@ public boolean supportsPooledSequences() {

@Override
public String getQuerySequencesString() {
return "select tabname from informix.systables where tabtype='Q'";
return "select systables.tabname as sequence_name, syssequences.* from syssequences join systables on syssequences.tabid = systables.tabid where tabtype = 'Q'";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
return SequenceInformationExtractorInformixDatabaseImpl.INSTANCE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.LimitHelper;
import org.hibernate.engine.spi.RowSelection;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorIngresDatabaseImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
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.tool.schema.extract.internal.SequenceNameExtractorImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

/**
Expand Down Expand Up @@ -215,6 +217,11 @@ public String getQuerySequencesString() {
return "select seq_name from iisequence";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
return SequenceNameExtractorImpl.INSTANCE;
}

@Override
public String getLowercaseFunction() {
return "lowercase";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.dialect;

import java.sql.Types;
import java.util.Locale;

Expand All @@ -15,6 +16,8 @@
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.LimitHelper;
import org.hibernate.engine.spi.RowSelection;
import org.hibernate.tool.schema.extract.internal.SequenceNameExtractorImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

/**
Expand Down Expand Up @@ -97,6 +100,11 @@ public String getQuerySequencesString() {
return "select RDB$GENERATOR_NAME from RDB$GENERATORS";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
return SequenceNameExtractorImpl.INSTANCE;
}

@Override
public String getForUpdateString() {
return " with lock";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import org.hibernate.LockOptions;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.tool.schema.extract.internal.SequenceNameExtractorImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

/**
Expand Down Expand Up @@ -59,6 +61,12 @@ public String getQuerySequencesString() {
return "select table_name from information_schema.TABLES where table_type='SEQUENCE'";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
//TODO: Future improvement - https://hibernate.atlassian.net/browse/HHH-13008
return SequenceNameExtractorImpl.INSTANCE;
}

@Override
public String getWriteLockString(int timeout) {
if ( timeout == LockOptions.NO_WAIT ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.dialect.identity.IdentityColumnSupport;
import org.hibernate.dialect.identity.MimerSQLIdentityColumnSupport;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorMimerSQLDatabaseImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

/**
Expand Down Expand Up @@ -166,7 +168,12 @@ public String getCascadeConstraintsString() {

@Override
public String getQuerySequencesString() {
return "select sequence_schema || '.' || sequence_name from information_schema.ext_sequences";
return "select * from information_schema.ext_sequences";
}

@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
return SequenceInformationExtractorMimerSQLDatabaseImpl.INSTANCE;
}

@Override
Expand Down
Loading

0 comments on commit 1db476d

Please sign in to comment.