Class BatchSequenceGenerator

java.lang.Object
io.hypersistence.utils.hibernate.id.BatchSequenceGenerator
All Implemented Interfaces:
Serializable, org.hibernate.boot.model.relational.ExportableProducer, org.hibernate.generator.BeforeExecutionGenerator, org.hibernate.generator.Generator, org.hibernate.id.BulkInsertionCapableIdentifierGenerator, org.hibernate.id.Configurable, org.hibernate.id.factory.spi.StandardGenerator, org.hibernate.id.IdentifierGenerator, org.hibernate.id.OptimizableGenerator, org.hibernate.id.PersistentIdentifierGenerator

public class BatchSequenceGenerator extends Object implements org.hibernate.id.BulkInsertionCapableIdentifierGenerator, org.hibernate.id.PersistentIdentifierGenerator, org.hibernate.id.Configurable
A sequence generator that uses a recursive query to fetch multiple values from a sequence in a single database access.

Configuration


 @Id
 @BatchSequence(name = "SOME_SEQUENCE_NAME", fetch_size = SOME_FETCH_SIZE_VALUE)
 private Long someColumnName;
 

SQL

Per default the generated SELECT will look something like this

 WITH RECURSIVE t(n) AS (
   SELECT 1
     UNION ALL
   SELECT n + 1
   FROM t
   WHERE n < ?)
 SELECT nextval(seq_xxx)
   FROM t;
 

DB2

For DB2 the generated SELECT will look something like this

 WITH t(n) AS (
   SELECT 1 AS n
     FROM (VALUES 1)
       UNION ALL
     SELECT n + 1 AS n
       FROM t
      WHERE n < ?)
 SELECT next value for SEQ_CHILD_ID AS n
   FROM t;
 

HSQLDB

For HSQLDB the generated SELECT will look something like this

 SELECT next value for seq_parent_id
   FROM UNNEST(SEQUENCE_ARRAY(1, ?, 1));
 

Oracle

For Oracle the generated SELECT will look something like this because Oracle does not support using recursive common table expressions to fetch multiple values from a sequence.

 SELECT seq_xxx.nextval
 FROM dual
 CONNECT BY rownum <= ?
 

SQL Server

For SQL Server the generated SELECT will look something like this

 WITH t(n) AS (
   SELECT 1 AS n
     UNION ALL
   SELECT n + 1 AS n
     FROM t
    WHERE n < ?)
 SELECT NEXT VALUE FOR seq_xxx AS n
   FROM t
 

Firebird

For Firebird the generated SELECT will look something like this

 WITH RECURSIVE t(n, level_num) AS (
   SELECT NEXT VALUE FOR seq_xxx AS n, 1 AS level_num
   FROM rdb$database
     UNION ALL
   SELECT NEXT VALUE FOR seq_xxx AS n, level_num + 1 AS level_num
     FROM t
    WHERE level_num < ?)
 SELECT n
   FROM t
 

Database Support

The following RDBMS have been verified to work
  • DB2
  • Firebird
  • Oracle
  • H2
  • HSQLDB
  • MariaDB
  • Postgres
  • SQL Sever

In theory any RDBMS that supports WITH RECURSIVE and sequences is supported.

For more details about how to use it, check out this article on vladmihalcea.com.

Since:
2.14.0
Author:
Philippe Marschall
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default value for FETCH_SIZE_PARAM.
    static final String
    Deprecated.
    static final String
    Deprecated.

    Fields inherited from interface org.hibernate.id.IdentifierGenerator

    CONTRIBUTOR_NAME, ENTITY_NAME, GENERATOR_NAME, JPA_ENTITY_NAME

    Fields inherited from interface org.hibernate.id.OptimizableGenerator

    DEFAULT_INCREMENT_SIZE, DEFAULT_INITIAL_VALUE, IMPLICIT_NAME_BASE, INCREMENT_PARAM, INITIAL_PARAM, OPT_PARAM

    Fields inherited from interface org.hibernate.id.PersistentIdentifierGenerator

    CATALOG, IDENTIFIER_NORMALIZER, PK, SCHEMA, TABLE, TABLES
  • Constructor Summary

    Constructors
    Constructor
    Description
    Called when GenericGenerator is used.
    BatchSequenceGenerator(BatchSequence annotation, Member annotatedMember, org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext context)
    Called when BatchSequence is used.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    configure(org.hibernate.type.Type type, Properties params, org.hibernate.service.ServiceRegistry serviceRegistry)
     
    determineBulkInsertionIdentifierGenerationSelectFragment(org.hibernate.boot.model.relational.SqlStringGenerationContext sqlStringGenerationContext)
     
    generate(org.hibernate.engine.spi.SharedSessionContractImplementor session, Object object)
     
    org.hibernate.id.enhanced.Optimizer
     
    void
    initialize(org.hibernate.boot.model.relational.SqlStringGenerationContext context)
     
    void
    registerExportables(org.hibernate.boot.model.relational.Database database)
     
    boolean
     
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.hibernate.generator.BeforeExecutionGenerator

    generatedOnExecution

    Methods inherited from interface org.hibernate.generator.Generator

    allowAssignedIdentifiers, generatedOnExecution, generatesOnInsert, generatesOnUpdate, generatesSometimes

    Methods inherited from interface org.hibernate.id.IdentifierGenerator

    generate, getEventTypes, supportsJdbcBatchInserts
  • Field Details

  • Constructor Details

    • BatchSequenceGenerator

      public BatchSequenceGenerator(BatchSequence annotation, Member annotatedMember, org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext context)
      Called when BatchSequence is used.
      Parameters:
      annotation - meta annotation for configuration
    • BatchSequenceGenerator

      public BatchSequenceGenerator()
      Called when GenericGenerator is used.
  • Method Details

    • configure

      public void configure(org.hibernate.type.Type type, Properties params, org.hibernate.service.ServiceRegistry serviceRegistry) throws org.hibernate.MappingException
      Specified by:
      configure in interface org.hibernate.id.Configurable
      Specified by:
      configure in interface org.hibernate.id.IdentifierGenerator
      Throws:
      org.hibernate.MappingException
    • initialize

      public void initialize(org.hibernate.boot.model.relational.SqlStringGenerationContext context)
      Specified by:
      initialize in interface org.hibernate.id.Configurable
    • supportsBulkInsertionIdentifierGeneration

      public boolean supportsBulkInsertionIdentifierGeneration()
      Specified by:
      supportsBulkInsertionIdentifierGeneration in interface org.hibernate.id.BulkInsertionCapableIdentifierGenerator
    • determineBulkInsertionIdentifierGenerationSelectFragment

      public String determineBulkInsertionIdentifierGenerationSelectFragment(org.hibernate.boot.model.relational.SqlStringGenerationContext sqlStringGenerationContext)
      Specified by:
      determineBulkInsertionIdentifierGenerationSelectFragment in interface org.hibernate.id.BulkInsertionCapableIdentifierGenerator
    • generate

      public Serializable generate(org.hibernate.engine.spi.SharedSessionContractImplementor session, Object object) throws org.hibernate.HibernateException
      Specified by:
      generate in interface org.hibernate.id.IdentifierGenerator
      Throws:
      org.hibernate.HibernateException
    • registerExportables

      public void registerExportables(org.hibernate.boot.model.relational.Database database)
      Specified by:
      registerExportables in interface org.hibernate.boot.model.relational.ExportableProducer
      Specified by:
      registerExportables in interface org.hibernate.id.IdentifierGenerator
    • getOptimizer

      public org.hibernate.id.enhanced.Optimizer getOptimizer()
      Specified by:
      getOptimizer in interface org.hibernate.id.OptimizableGenerator
    • toString

      public String toString()
      Overrides:
      toString in class Object