HHH-10274 - org.hibernate.id.SequenceValueExtractor does not work for DB2, Oracle, or SQL Server

This commit is contained in:
Andrea Boriero 2015-11-13 16:59:30 +00:00
parent 9372803718
commit 765c7020eb
3 changed files with 17 additions and 32 deletions

View File

@ -17,10 +17,8 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.exception.GenericJDBCException;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.internal.SessionImpl;
import org.hibernate.type.StandardBasicTypes;
@ -102,20 +100,6 @@ public class SequenceHiLoGeneratorNoIncrementTest extends BaseUnitTestCase {
public void testHiLoAlgorithm() {
sessionImpl = (SessionImpl) sessionFactory.openSession();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// initially sequence should be uninitialized
if ( sessionFactory.getDialect() instanceof PostgreSQL81Dialect ) {
try {
assertEquals( 0L, extractSequenceValue() );
}
catch (GenericJDBCException ge) {
// PostgreSQL throws an exception if currval is called before nextval for this sequence in this session
}
}
else {
assertEquals( 0L, extractSequenceValue() );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// historically the hilo generators skipped the initial block of values;
// so the first generated id value is maxlo + 1, here be 4

View File

@ -16,10 +16,8 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.exception.GenericJDBCException;
import org.hibernate.internal.SessionImpl;
import org.hibernate.type.StandardBasicTypes;
@ -92,20 +90,6 @@ public class SequenceHiLoGeneratorTest extends BaseUnitTestCase {
public void testHiLoAlgorithm() {
sessionImpl = (SessionImpl) sessionFactory.openSession();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// initially sequence should be uninitialized
if ( sessionFactory.getDialect() instanceof PostgreSQL81Dialect ) {
try {
assertEquals( 0L, extractSequenceValue() );
}
catch (GenericJDBCException ge) {
// PostgreSQL throws an exception if currval is called before nextval for this sequence in this session
}
}
else {
assertEquals( 0L, extractSequenceValue() );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// historically the hilo generators skipped the initial block of values;
// so the first generated id value is maxlo + 1, here be 4

View File

@ -13,8 +13,12 @@ import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.exception.GenericJDBCException;
import org.hibernate.jdbc.Work;
@ -32,6 +36,19 @@ public class SequenceValueExtractor {
if ( dialect instanceof DerbyDialect ) {
queryString = "VALUES SYSCS_UTIL.SYSCS_PEEK_AT_SEQUENCE('HIBERNATE_ORM_TEST', '" + sequenceName.toUpperCase() + "')";
}
else if ( dialect instanceof DB2Dialect ) {
queryString = "values PREVIOUS value for " + sequenceName;
}
else if ( dialect instanceof Oracle8iDialect ) {
queryString = "select " + sequenceName + ".currval from dual";
}
else if ( dialect instanceof SQLServer2012Dialect ) {
queryString = "SELECT CONVERT(varchar(200), Current_value) FROM SYS.Sequences WHERE name = '" + sequenceName + "'";
}
else if ( dialect instanceof HSQLDialect ) {
queryString = "call current value for " + sequenceName;
}
else {
queryString = "select currval('" + sequenceName + "');";
}