com.vladmihalcea.hibernate.id
Class BatchSequenceGenerator

java.lang.Object
  extended by com.vladmihalcea.hibernate.id.BatchSequenceGenerator
All Implemented Interfaces:
org.hibernate.id.BulkInsertionCapableIdentifierGenerator, org.hibernate.id.Configurable, org.hibernate.id.IdentifierGenerator, 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.

Parameters

The following configuration parameters are supported:
"sequence"
mandatory, name of the sequence to use
"fetch_size"
optional, how many sequence numbers should be fetched at a time, default is 10

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

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

Field Summary
static int DEFAULT_FETCH_SIZE
          The default value for FETCH_SIZE_PARAM.
static String FETCH_SIZE_PARAM
          Indicates how many sequence values to fetch at once.
static String SEQUENCE_PARAM
          Indicates the name of the sequence to use, mandatory.
 
Fields inherited from interface org.hibernate.id.PersistentIdentifierGenerator
CATALOG, IDENTIFIER_NORMALIZER, PK, SCHEMA, TABLE, TABLES
 
Fields inherited from interface org.hibernate.id.IdentifierGenerator
ENTITY_NAME, JPA_ENTITY_NAME
 
Constructor Summary
BatchSequenceGenerator()
           
 
Method Summary
 void configure(org.hibernate.type.Type type, Properties params, org.hibernate.dialect.Dialect dialect)
           
 String determineBulkInsertionIdentifierGenerationSelectFragment(org.hibernate.dialect.Dialect dialect)
           
 Serializable generate(org.hibernate.engine.spi.SessionImplementor session, Object object)
           
 Object generatorKey()
           
 String[] sqlCreateStrings(org.hibernate.dialect.Dialect dialect)
          Deprecated. 
 String[] sqlDropStrings(org.hibernate.dialect.Dialect dialect)
          Deprecated. 
 boolean supportsBulkInsertionIdentifierGeneration()
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SEQUENCE_PARAM

public static final String SEQUENCE_PARAM
Indicates the name of the sequence to use, mandatory.

See Also:
Constant Field Values

FETCH_SIZE_PARAM

public static final String FETCH_SIZE_PARAM
Indicates how many sequence values to fetch at once. The default value is DEFAULT_FETCH_SIZE.

See Also:
Constant Field Values

DEFAULT_FETCH_SIZE

public static final int DEFAULT_FETCH_SIZE
The default value for FETCH_SIZE_PARAM.

See Also:
Constant Field Values
Constructor Detail

BatchSequenceGenerator

public BatchSequenceGenerator()
Method Detail

configure

public void configure(org.hibernate.type.Type type,
                      Properties params,
                      org.hibernate.dialect.Dialect dialect)
               throws org.hibernate.MappingException
Specified by:
configure in interface org.hibernate.id.Configurable
Throws:
org.hibernate.MappingException

supportsBulkInsertionIdentifierGeneration

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

determineBulkInsertionIdentifierGenerationSelectFragment

public String determineBulkInsertionIdentifierGenerationSelectFragment(org.hibernate.dialect.Dialect dialect)
Specified by:
determineBulkInsertionIdentifierGenerationSelectFragment in interface org.hibernate.id.BulkInsertionCapableIdentifierGenerator

generate

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

generatorKey

public Object generatorKey()
Specified by:
generatorKey in interface org.hibernate.id.PersistentIdentifierGenerator

sqlCreateStrings

@Deprecated
public String[] sqlCreateStrings(org.hibernate.dialect.Dialect dialect)
Deprecated. 

Specified by:
sqlCreateStrings in interface org.hibernate.id.PersistentIdentifierGenerator

sqlDropStrings

@Deprecated
public String[] sqlDropStrings(org.hibernate.dialect.Dialect dialect)
Deprecated. 

Specified by:
sqlDropStrings in interface org.hibernate.id.PersistentIdentifierGenerator

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2022. All rights reserved.