1919import org .hibernate .MappingException ;
2020import org .hibernate .boot .model .relational .Database ;
2121import org .hibernate .boot .model .relational .QualifiedNameParser ;
22+ import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
2223import org .hibernate .dialect .Dialect ;
2324import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
2425import org .hibernate .engine .jdbc .spi .JdbcCoordinator ;
2526import org .hibernate .engine .spi .SharedSessionContractImplementor ;
2627import org .hibernate .id .BulkInsertionCapableIdentifierGenerator ;
27- import org .hibernate .id .Configurable ;
2828import org .hibernate .id .IdentifierGenerationException ;
2929import org .hibernate .id .IdentifierGenerator ;
3030import org .hibernate .id .PersistentIdentifierGenerator ;
3131import org .hibernate .id .enhanced .DatabaseStructure ;
32+ import org .hibernate .id .enhanced .NoopOptimizer ;
33+ import org .hibernate .id .enhanced .Optimizer ;
3234import org .hibernate .id .enhanced .SequenceStructure ;
3335import org .hibernate .internal .util .config .ConfigurationHelper ;
3436import org .hibernate .service .ServiceRegistry ;
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 }
0 commit comments