From 765c7020eb3ac735aa3ee3d60a0909cfbf13d4c9 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 13 Nov 2015 16:59:30 +0000 Subject: [PATCH] HHH-10274 - org.hibernate.id.SequenceValueExtractor does not work for DB2, Oracle, or SQL Server --- .../SequenceHiLoGeneratorNoIncrementTest.java | 16 ---------------- .../hibernate/id/SequenceHiLoGeneratorTest.java | 16 ---------------- .../hibernate/id/SequenceValueExtractor.java | 17 +++++++++++++++++ 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorNoIncrementTest.java b/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorNoIncrementTest.java index 4888488951..83340c7547 100644 --- a/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorNoIncrementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorNoIncrementTest.java @@ -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 diff --git a/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java index 7e3f9c6e31..12560f720d 100644 --- a/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java @@ -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 diff --git a/hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java b/hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java index 93ae716080..c001134fc5 100644 --- a/hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java +++ b/hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java @@ -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 + "');"; }