Package com.vladmihalcea.hibernate.id
Class BatchSequenceGenerator
- java.lang.Object
-
- com.vladmihalcea.hibernate.id.BatchSequenceGenerator
-
- All Implemented Interfaces:
org.hibernate.boot.model.relational.ExportableProducer
,org.hibernate.id.BulkInsertionCapableIdentifierGenerator
,org.hibernate.id.Configurable
,org.hibernate.id.IdentifierGenerator
,org.hibernate.id.PersistentIdentifierGenerator
public class BatchSequenceGenerator extends java.lang.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 thisWITH 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 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
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
BatchSequenceGenerator.IdentifierExtractor
Extracts aSerializable
identifier from aResultSet
.(package private) static class
BatchSequenceGenerator.IdentifierPool
Holds a number of prefetched identifiers.
-
Field Summary
Fields Modifier and Type Field Description private org.hibernate.id.enhanced.DatabaseStructure
databaseStructure
static int
DEFAULT_FETCH_SIZE
The default value forFETCH_SIZE_PARAM
.static java.lang.String
FETCH_SIZE_PARAM
Indicates how many sequence values to fetch at once.private int
fetchSize
private BatchSequenceGenerator.IdentifierExtractor
identifierExtractor
private BatchSequenceGenerator.IdentifierPool
identifierPool
private java.util.concurrent.locks.Lock
lock
private java.lang.String
select
static java.lang.String
SEQUENCE_PARAM
Indicates the name of the sequence to use, mandatory.
-
Constructor Summary
Constructors Constructor Description BatchSequenceGenerator()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private org.hibernate.id.enhanced.SequenceStructure
buildDatabaseStructure(org.hibernate.type.Type type, java.lang.String sequenceName, org.hibernate.engine.jdbc.env.spi.JdbcEnvironment jdbcEnvironment)
private static java.lang.String
buildSelect(java.lang.String sequenceName, org.hibernate.dialect.Dialect dialect)
void
configure(org.hibernate.type.Type type, java.util.Properties params, org.hibernate.service.ServiceRegistry serviceRegistry)
java.lang.String
determineBulkInsertionIdentifierGenerationSelectFragment(org.hibernate.dialect.Dialect dialect)
private static int
determineFetchSize(java.util.Properties params)
private static java.lang.String
determineSequenceName(java.util.Properties params)
java.io.Serializable
generate(org.hibernate.engine.spi.SharedSessionContractImplementor session, java.lang.Object object)
java.lang.Object
generatorKey()
private java.lang.String
getSequenceName()
void
registerExportables(org.hibernate.boot.model.relational.Database database)
private BatchSequenceGenerator.IdentifierPool
replenishIdentifierPool(org.hibernate.engine.spi.SharedSessionContractImplementor session)
java.lang.String[]
sqlCreateStrings(org.hibernate.dialect.Dialect dialect)
Deprecated.java.lang.String[]
sqlDropStrings(org.hibernate.dialect.Dialect dialect)
Deprecated.boolean
supportsBulkInsertionIdentifierGeneration()
java.lang.String
toString()
-
-
-
Field Detail
-
SEQUENCE_PARAM
public static final java.lang.String SEQUENCE_PARAM
Indicates the name of the sequence to use, mandatory.- See Also:
- Constant Field Values
-
FETCH_SIZE_PARAM
public static final java.lang.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
-
lock
private final java.util.concurrent.locks.Lock lock
-
select
private java.lang.String select
-
fetchSize
private int fetchSize
-
identifierPool
private BatchSequenceGenerator.IdentifierPool identifierPool
-
identifierExtractor
private BatchSequenceGenerator.IdentifierExtractor identifierExtractor
-
databaseStructure
private org.hibernate.id.enhanced.DatabaseStructure databaseStructure
-
-
Method Detail
-
configure
public void configure(org.hibernate.type.Type type, java.util.Properties params, org.hibernate.service.ServiceRegistry serviceRegistry) throws org.hibernate.MappingException
- Specified by:
configure
in interfaceorg.hibernate.id.Configurable
- Throws:
org.hibernate.MappingException
-
buildSelect
private static java.lang.String buildSelect(java.lang.String sequenceName, org.hibernate.dialect.Dialect dialect)
-
buildDatabaseStructure
private org.hibernate.id.enhanced.SequenceStructure buildDatabaseStructure(org.hibernate.type.Type type, java.lang.String sequenceName, org.hibernate.engine.jdbc.env.spi.JdbcEnvironment jdbcEnvironment)
-
determineSequenceName
private static java.lang.String determineSequenceName(java.util.Properties params)
-
determineFetchSize
private static int determineFetchSize(java.util.Properties params)
-
supportsBulkInsertionIdentifierGeneration
public boolean supportsBulkInsertionIdentifierGeneration()
- Specified by:
supportsBulkInsertionIdentifierGeneration
in interfaceorg.hibernate.id.BulkInsertionCapableIdentifierGenerator
-
determineBulkInsertionIdentifierGenerationSelectFragment
public java.lang.String determineBulkInsertionIdentifierGenerationSelectFragment(org.hibernate.dialect.Dialect dialect)
- Specified by:
determineBulkInsertionIdentifierGenerationSelectFragment
in interfaceorg.hibernate.id.BulkInsertionCapableIdentifierGenerator
-
generate
public java.io.Serializable generate(org.hibernate.engine.spi.SharedSessionContractImplementor session, java.lang.Object object) throws org.hibernate.HibernateException
- Specified by:
generate
in interfaceorg.hibernate.id.IdentifierGenerator
- Throws:
org.hibernate.HibernateException
-
generatorKey
public java.lang.Object generatorKey()
- Specified by:
generatorKey
in interfaceorg.hibernate.id.PersistentIdentifierGenerator
-
getSequenceName
private java.lang.String getSequenceName()
-
sqlCreateStrings
@Deprecated public java.lang.String[] sqlCreateStrings(org.hibernate.dialect.Dialect dialect)
Deprecated.- Specified by:
sqlCreateStrings
in interfaceorg.hibernate.id.PersistentIdentifierGenerator
-
sqlDropStrings
@Deprecated public java.lang.String[] sqlDropStrings(org.hibernate.dialect.Dialect dialect)
Deprecated.- Specified by:
sqlDropStrings
in interfaceorg.hibernate.id.PersistentIdentifierGenerator
-
registerExportables
public void registerExportables(org.hibernate.boot.model.relational.Database database)
- Specified by:
registerExportables
in interfaceorg.hibernate.boot.model.relational.ExportableProducer
-
replenishIdentifierPool
private BatchSequenceGenerator.IdentifierPool replenishIdentifierPool(org.hibernate.engine.spi.SharedSessionContractImplementor session) throws org.hibernate.HibernateException
- Throws:
org.hibernate.HibernateException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-