Class BatchSequenceGenerator
- java.lang.Object
-
- com.github.marschall.hibernate.batchsequencegenerator.BatchSequenceGenerator
-
- All Implemented Interfaces:
ExportableProducer
,BulkInsertionCapableIdentifierGenerator
,Configurable
,StandardGenerator
,IdentifierGenerator
,OptimizableGenerator
,PersistentIdentifierGenerator
public class BatchSequenceGenerator extends Object implements BulkInsertionCapableIdentifierGenerator, PersistentIdentifierGenerator
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 thisWITH RECURSIVE t(n, level_num) AS ( SELECT nextval(seq_xxx) as n, 1 as level_num UNION ALL SELECT nextval(seq_xxx) as n, level_num + 1 as level_num FROM t WHERE level_num < ?) SELECT n FROM t;
DB2
For DB2 the generated SELECT will look something like thisWITH 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 thisSELECT 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 thisWITH 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 thisWITH 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
WITH RECURSIVE
and sequences is supported.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_FETCH_SIZE
The default value forFETCH_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.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 BatchSequenceGenerator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configure(Type type, Properties params, ServiceRegistry serviceRegistry)
String
determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext context)
Serializable
generate(SharedSessionContractImplementor session, Object object)
Optimizer
getOptimizer()
void
registerExportables(Database database)
boolean
supportsBulkInsertionIdentifierGeneration()
boolean
supportsJdbcBatchInserts()
String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.hibernate.id.IdentifierGenerator
initialize
-
-
-
-
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 isDEFAULT_FETCH_SIZE
.- See Also:
- Constant Field Values
-
DEFAULT_FETCH_SIZE
public static final int DEFAULT_FETCH_SIZE
The default value forFETCH_SIZE_PARAM
.- See Also:
- Constant Field Values
-
-
Method Detail
-
configure
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry)
- Specified by:
configure
in interfaceConfigurable
- Specified by:
configure
in interfaceIdentifierGenerator
-
supportsBulkInsertionIdentifierGeneration
public boolean supportsBulkInsertionIdentifierGeneration()
- Specified by:
supportsBulkInsertionIdentifierGeneration
in interfaceBulkInsertionCapableIdentifierGenerator
-
supportsJdbcBatchInserts
public boolean supportsJdbcBatchInserts()
- Specified by:
supportsJdbcBatchInserts
in interfaceIdentifierGenerator
-
determineBulkInsertionIdentifierGenerationSelectFragment
public String determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext context)
- Specified by:
determineBulkInsertionIdentifierGenerationSelectFragment
in interfaceBulkInsertionCapableIdentifierGenerator
-
generate
public Serializable generate(SharedSessionContractImplementor session, Object object)
- Specified by:
generate
in interfaceIdentifierGenerator
-
getOptimizer
public Optimizer getOptimizer()
- Specified by:
getOptimizer
in interfaceOptimizableGenerator
-
registerExportables
public void registerExportables(Database database)
- Specified by:
registerExportables
in interfaceExportableProducer
- Specified by:
registerExportables
in interfaceIdentifierGenerator
-
-