Skip to content

Commit 765c702

Browse files
committed
HHH-10274 - org.hibernate.id.SequenceValueExtractor does not work for DB2, Oracle, or SQL Server
1 parent 9372803 commit 765c702

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorNoIncrementTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
1818
import org.hibernate.boot.spi.MetadataBuildingContext;
1919
import org.hibernate.cfg.AvailableSettings;
20-
import org.hibernate.dialect.PostgreSQL81Dialect;
2120
import org.hibernate.engine.spi.SessionFactoryImplementor;
2221
import org.hibernate.engine.spi.SessionImplementor;
23-
import org.hibernate.exception.GenericJDBCException;
2422
import org.hibernate.id.enhanced.SequenceStyleGenerator;
2523
import org.hibernate.internal.SessionImpl;
2624
import org.hibernate.type.StandardBasicTypes;
@@ -102,20 +100,6 @@ public void tearDown() throws Exception {
102100
public void testHiLoAlgorithm() {
103101
sessionImpl = (SessionImpl) sessionFactory.openSession();
104102

105-
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106-
// initially sequence should be uninitialized
107-
if ( sessionFactory.getDialect() instanceof PostgreSQL81Dialect ) {
108-
try {
109-
assertEquals( 0L, extractSequenceValue() );
110-
}
111-
catch (GenericJDBCException ge) {
112-
// PostgreSQL throws an exception if currval is called before nextval for this sequence in this session
113-
}
114-
}
115-
else {
116-
assertEquals( 0L, extractSequenceValue() );
117-
}
118-
119103
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120104
// historically the hilo generators skipped the initial block of values;
121105
// so the first generated id value is maxlo + 1, here be 4

hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
1717
import org.hibernate.boot.spi.MetadataBuildingContext;
1818
import org.hibernate.cfg.AvailableSettings;
19-
import org.hibernate.dialect.PostgreSQL81Dialect;
2019
import org.hibernate.engine.spi.SessionFactoryImplementor;
2120
import org.hibernate.engine.spi.SessionImplementor;
22-
import org.hibernate.exception.GenericJDBCException;
2321
import org.hibernate.internal.SessionImpl;
2422
import org.hibernate.type.StandardBasicTypes;
2523

@@ -92,20 +90,6 @@ public void tearDown() throws Exception {
9290
public void testHiLoAlgorithm() {
9391
sessionImpl = (SessionImpl) sessionFactory.openSession();
9492

95-
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96-
// initially sequence should be uninitialized
97-
if ( sessionFactory.getDialect() instanceof PostgreSQL81Dialect ) {
98-
try {
99-
assertEquals( 0L, extractSequenceValue() );
100-
}
101-
catch (GenericJDBCException ge) {
102-
// PostgreSQL throws an exception if currval is called before nextval for this sequence in this session
103-
}
104-
}
105-
else {
106-
assertEquals( 0L, extractSequenceValue() );
107-
}
108-
10993
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11094
// historically the hilo generators skipped the initial block of values;
11195
// so the first generated id value is maxlo + 1, here be 4

hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
import org.hibernate.Session;
1515
import org.hibernate.Transaction;
16+
import org.hibernate.dialect.DB2Dialect;
1617
import org.hibernate.dialect.DerbyDialect;
1718
import org.hibernate.dialect.Dialect;
19+
import org.hibernate.dialect.HSQLDialect;
20+
import org.hibernate.dialect.Oracle8iDialect;
21+
import org.hibernate.dialect.SQLServer2012Dialect;
1822
import org.hibernate.engine.spi.SessionImplementor;
1923
import org.hibernate.exception.GenericJDBCException;
2024
import org.hibernate.jdbc.Work;
@@ -32,6 +36,19 @@ public SequenceValueExtractor(Dialect dialect, String sequenceName) {
3236
if ( dialect instanceof DerbyDialect ) {
3337
queryString = "VALUES SYSCS_UTIL.SYSCS_PEEK_AT_SEQUENCE('HIBERNATE_ORM_TEST', '" + sequenceName.toUpperCase() + "')";
3438
}
39+
else if ( dialect instanceof DB2Dialect ) {
40+
queryString = "values PREVIOUS value for " + sequenceName;
41+
}
42+
else if ( dialect instanceof Oracle8iDialect ) {
43+
queryString = "select " + sequenceName + ".currval from dual";
44+
}
45+
else if ( dialect instanceof SQLServer2012Dialect ) {
46+
queryString = "SELECT CONVERT(varchar(200), Current_value) FROM SYS.Sequences WHERE name = '" + sequenceName + "'";
47+
}
48+
else if ( dialect instanceof HSQLDialect ) {
49+
50+
queryString = "call current value for " + sequenceName;
51+
}
3552
else {
3653
queryString = "select currval('" + sequenceName + "');";
3754
}

0 commit comments

Comments
 (0)