Skip to content

Commit 012a5a3

Browse files
committed
updates for latest Hibernate
1 parent 37e14b8 commit 012a5a3

File tree

3 files changed

+70
-73
lines changed

3 files changed

+70
-73
lines changed

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,21 @@
209209
<dependency>
210210
<groupId>org.springframework</groupId>
211211
<artifactId>spring-framework-bom</artifactId>
212-
<version>5.3.14</version>
212+
<version>5.3.15</version>
213213
<type>pom</type>
214214
<scope>import</scope>
215215
</dependency>
216216
<dependency>
217217
<groupId>com.oracle.database.jdbc</groupId>
218218
<artifactId>ojdbc-bom</artifactId>
219-
<version>21.3.0.0</version>
219+
<version>21.5.0.0</version>
220220
<type>pom</type>
221221
<scope>import</scope>
222222
</dependency>
223223
<dependency>
224224
<groupId>org.apache.logging.log4j</groupId>
225225
<artifactId>log4j-bom</artifactId>
226-
<version>2.16.0</version>
226+
<version>2.17.1</version>
227227
<type>pom</type>
228228
<scope>import</scope>
229229
</dependency>
@@ -275,7 +275,7 @@
275275
<dependency>
276276
<groupId>org.postgresql</groupId>
277277
<artifactId>postgresql</artifactId>
278-
<version>42.3.1</version>
278+
<version>42.3.2</version>
279279
<scope>test</scope>
280280
</dependency>
281281
<dependency>
@@ -287,19 +287,19 @@
287287
<dependency>
288288
<groupId>com.microsoft.sqlserver</groupId>
289289
<artifactId>mssql-jdbc</artifactId>
290-
<version>9.4.0.jre11</version>
290+
<version>10.2.0.jre11</version>
291291
<scope>test</scope>
292292
</dependency>
293293
<dependency>
294294
<groupId>org.firebirdsql.jdbc</groupId>
295295
<artifactId>jaybird</artifactId>
296-
<version>4.0.4.java11</version>
296+
<version>4.0.5.java11</version>
297297
<scope>test</scope>
298298
</dependency>
299299
<dependency>
300300
<groupId>org.mariadb.jdbc</groupId>
301301
<artifactId>mariadb-java-client</artifactId>
302-
<version>2.7.4</version>
302+
<version>3.0.3</version>
303303
<scope>test</scope>
304304
</dependency>
305305
<dependency>
@@ -394,7 +394,7 @@
394394
<properties>
395395
<project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
396396
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
397-
<hibernate.version>6.0.0.Beta2</hibernate.version>
397+
<hibernate.version>6.0.0.CR1</hibernate.version>
398398
</properties>
399399

400400
</project>

src/main/java/com/github/marschall/hibernate/batchsequencegenerator/BatchSequenceGenerator.java

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
import org.hibernate.MappingException;
2020
import org.hibernate.boot.model.relational.Database;
2121
import org.hibernate.boot.model.relational.QualifiedNameParser;
22+
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
2223
import org.hibernate.dialect.Dialect;
2324
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
2425
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
2526
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2627
import org.hibernate.id.BulkInsertionCapableIdentifierGenerator;
27-
import org.hibernate.id.Configurable;
2828
import org.hibernate.id.IdentifierGenerationException;
2929
import org.hibernate.id.IdentifierGenerator;
3030
import org.hibernate.id.PersistentIdentifierGenerator;
3131
import org.hibernate.id.enhanced.DatabaseStructure;
32+
import org.hibernate.id.enhanced.NoopOptimizer;
33+
import org.hibernate.id.enhanced.Optimizer;
3234
import org.hibernate.id.enhanced.SequenceStructure;
3335
import org.hibernate.internal.util.config.ConfigurationHelper;
3436
import org.hibernate.service.ServiceRegistry;
@@ -134,7 +136,7 @@
134136
* In theory any RDBMS that supports {@code WITH RECURSIVE} and
135137
* sequences is supported.
136138
*/
137-
public class BatchSequenceGenerator implements BulkInsertionCapableIdentifierGenerator, PersistentIdentifierGenerator, Configurable {
139+
public class BatchSequenceGenerator implements BulkInsertionCapableIdentifierGenerator, PersistentIdentifierGenerator {
138140

139141
/**
140142
* Indicates the name of the sequence to use, mandatory.
@@ -158,19 +160,21 @@ public class BatchSequenceGenerator implements BulkInsertionCapableIdentifierGen
158160
private IdentifierPool identifierPool;
159161
private IdentifierExtractor identifierExtractor;
160162
private DatabaseStructure databaseStructure;
163+
private NoopOptimizer optimizer;
161164

162165
@Override
163-
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry)
164-
throws MappingException {
166+
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) {
165167

166168
JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class);
167169
Dialect dialect = jdbcEnvironment.getDialect();
168170
String sequenceName = determineSequenceName(params);
169171
this.fetchSize = determineFetchSize(params);
170172

171173
this.select = buildSelect(sequenceName, dialect);
172-
this.identifierExtractor = IdentifierExtractor.getIdentifierExtractor(type.getReturnedClass());
174+
Class<?> returnedClass = type.getReturnedClass();
175+
this.identifierExtractor = IdentifierExtractor.getIdentifierExtractor(returnedClass);
173176
this.identifierPool = IdentifierPool.empty();
177+
this.optimizer = new NoopOptimizer(returnedClass, 1);
174178

175179
this.databaseStructure = this.buildDatabaseStructure(type, sequenceName, jdbcEnvironment, params);
176180
}
@@ -255,12 +259,17 @@ public boolean supportsBulkInsertionIdentifierGeneration() {
255259
}
256260

257261
@Override
258-
public String determineBulkInsertionIdentifierGenerationSelectFragment(Dialect dialect) {
259-
return dialect.getSequenceSupport().getSelectSequenceNextValString(this.getSequenceName());
262+
public boolean supportsJdbcBatchInserts() {
263+
return true;
264+
}
265+
266+
@Override
267+
public String determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext context) {
268+
return context.getDialect().getSequenceSupport().getSelectSequenceNextValString(this.getSequenceName());
260269
}
261270

262271
@Override
263-
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
272+
public Serializable generate(SharedSessionContractImplementor session, Object object) {
264273
this.lock.lock();
265274
try {
266275
if (this.identifierPool.isEmpty()) {
@@ -273,24 +282,12 @@ public Serializable generate(SharedSessionContractImplementor session, Object ob
273282
}
274283

275284
@Override
276-
public Object generatorKey() {
277-
return this.getSequenceName();
285+
public Optimizer getOptimizer() {
286+
return this.optimizer;
278287
}
279288

280289
private String getSequenceName() {
281-
return this.databaseStructure.getName();
282-
}
283-
284-
@Override
285-
@Deprecated
286-
public String[] sqlCreateStrings(Dialect dialect) {
287-
return this.databaseStructure.sqlCreateStrings(dialect);
288-
}
289-
290-
@Override
291-
@Deprecated
292-
public String[] sqlDropStrings(Dialect dialect) {
293-
return this.databaseStructure.sqlDropStrings(dialect);
290+
return this.databaseStructure.getPhysicalName().getObjectName().getCanonicalName();
294291
}
295292

296293
@Override
@@ -355,55 +352,55 @@ Serializable next() {
355352
*
356353
* @see org.hibernate.id.IntegralDataTypeHolder
357354
*/
358-
enum IdentifierExtractor {
359-
360-
INTEGER_IDENTIFIER_EXTRACTOR {
361-
@Override
362-
Serializable extractIdentifier(ResultSet resultSet) throws SQLException {
363-
int intValue = resultSet.getInt(1);
364-
if (resultSet.wasNull()) {
365-
throw new IdentifierGenerationException("sequence returned null");
366-
}
367-
return intValue;
355+
@FunctionalInterface
356+
interface IdentifierExtractor {
357+
358+
IdentifierExtractor SHORT_IDENTIFIER_EXTRACTOR = (ResultSet resultSet) -> {
359+
short shortValue = resultSet.getShort(1);
360+
if (resultSet.wasNull()) {
361+
throw new IdentifierGenerationException("sequence returned null");
368362
}
369-
},
370-
371-
LONG_IDENTIFIER_EXTRACTOR {
372-
@Override
373-
Serializable extractIdentifier(ResultSet resultSet) throws SQLException {
374-
long longValue = resultSet.getLong(1);
375-
if (resultSet.wasNull()) {
376-
throw new IdentifierGenerationException("sequence returned null");
377-
}
378-
return longValue;
363+
return shortValue;
364+
};
365+
366+
IdentifierExtractor INTEGER_IDENTIFIER_EXTRACTOR = (ResultSet resultSet) -> {
367+
int intValue = resultSet.getInt(1);
368+
if (resultSet.wasNull()) {
369+
throw new IdentifierGenerationException("sequence returned null");
379370
}
380-
},
381-
382-
BIG_INTEGER_IDENTIFIER_EXTRACTOR {
383-
@Override
384-
Serializable extractIdentifier(ResultSet resultSet) throws SQLException {
385-
BigDecimal bigDecimal = resultSet.getBigDecimal(1);
386-
if (resultSet.wasNull()) {
387-
throw new IdentifierGenerationException("sequence returned null");
388-
}
389-
return bigDecimal.setScale(0, RoundingMode.UNNECESSARY).toBigInteger();
371+
return intValue;
372+
};
373+
374+
IdentifierExtractor LONG_IDENTIFIER_EXTRACTOR = (ResultSet resultSet) -> {
375+
long longValue = resultSet.getLong(1);
376+
if (resultSet.wasNull()) {
377+
throw new IdentifierGenerationException("sequence returned null");
390378
}
391-
},
392-
393-
BIG_DECIMAL_IDENTIFIER_EXTRACTOR {
394-
@Override
395-
Serializable extractIdentifier(ResultSet resultSet) throws SQLException {
396-
BigDecimal bigDecimal = resultSet.getBigDecimal(1);
397-
if (resultSet.wasNull()) {
398-
throw new IdentifierGenerationException("sequence returned null");
399-
}
400-
return bigDecimal;
379+
return longValue;
380+
};
381+
382+
IdentifierExtractor BIG_INTEGER_IDENTIFIER_EXTRACTOR = (ResultSet resultSet) -> {
383+
BigDecimal bigDecimal = resultSet.getBigDecimal(1);
384+
if (resultSet.wasNull()) {
385+
throw new IdentifierGenerationException("sequence returned null");
386+
}
387+
return bigDecimal.setScale(0, RoundingMode.UNNECESSARY).toBigInteger();
388+
};
389+
390+
IdentifierExtractor BIG_DECIMAL_IDENTIFIER_EXTRACTOR = (ResultSet resultSet) -> {
391+
BigDecimal bigDecimal = resultSet.getBigDecimal(1);
392+
if (resultSet.wasNull()) {
393+
throw new IdentifierGenerationException("sequence returned null");
401394
}
395+
return bigDecimal;
402396
};
403397

404-
abstract Serializable extractIdentifier(ResultSet resultSet) throws SQLException;
398+
Serializable extractIdentifier(ResultSet resultSet) throws SQLException;
405399

406400
static IdentifierExtractor getIdentifierExtractor(Class<?> integralType) {
401+
if ((integralType == Short.class) || (integralType == short.class)) {
402+
return SHORT_IDENTIFIER_EXTRACTOR;
403+
}
407404
if ((integralType == Integer.class) || (integralType == int.class)) {
408405
return INTEGER_IDENTIFIER_EXTRACTOR;
409406
}

src/test/java/com/github/marschall/hibernate/batchsequencegenerator/configurations/SqlServerConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public DataSource dataSource() {
2929
SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
3030
dataSource.setSuppressClose(true);
3131
// https://github.com/microsoft/mssql-jdbc/issues/1182
32-
dataSource.setUrl("jdbc:sqlserver://localhost:1433;databaseName=master;sendTimeAsDatetime=false");
32+
dataSource.setUrl("jdbc:sqlserver://localhost:1433;databaseName=master;sendTimeAsDatetime=false;encrypt=false");
3333
dataSource.setUsername("sa");
3434
dataSource.setPassword("Cent-Quick-Space-Bath-8");
3535
return dataSource;

0 commit comments

Comments
 (0)