Class IdentityGenerator
- All Implemented Interfaces:
Serializable
,Generator
,OnExecutionGenerator
,BulkInsertionCapableIdentifierGenerator
,Configurable
,PostInsertIdentifierGenerator
OnExecutionGenerator
that handles IDENTITY
/"autoincrement"
columns on those databases which support them.
Delegates to the IdentityColumnSupport
provided by the dialect.
The actual work involved in retrieving the primary key value is the job of a
GeneratedValuesMutationDelegate
.
- See Also:
- Implementation Note:
- This also implements the
identity
generation type inhbm.xml
mappings.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetGeneratedIdentifierDelegate
(EntityPersister persister) TheInsertGeneratedIdentifierDelegate
used to retrieve the generated value if this object is an identifier generator.String[]
getReferencedColumnValues
(Dialect dialect) A SQL expression indicating how to calculate the generated values when the mapped columns are included in the SQL statement.boolean
referenceColumnsInSql
(Dialect dialect) Determines if the columns whose values are generated are included in the column list of the SQLinsert
orupdate
statement.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.hibernate.id.BulkInsertionCapableIdentifierGenerator
determineBulkInsertionIdentifierGenerationSelectFragment, supportsBulkInsertionIdentifierGeneration
Methods inherited from interface org.hibernate.id.Configurable
configure, initialize
Methods inherited from interface org.hibernate.generator.Generator
allowAssignedIdentifiers, allowMutation, generatedBeforeExecution, generatedOnExecution, generatesOnInsert, generatesOnUpdate, generatesSometimes
Methods inherited from interface org.hibernate.generator.OnExecutionGenerator
generatedOnExecution, getUniqueKeyPropertyNames
Methods inherited from interface org.hibernate.id.PostInsertIdentifierGenerator
configure, getEventTypes, writePropertyValue
-
Constructor Details
-
IdentityGenerator
public IdentityGenerator()
-
-
Method Details
-
referenceColumnsInSql
Description copied from interface:OnExecutionGenerator
Determines if the columns whose values are generated are included in the column list of the SQLinsert
orupdate
statement. For example, this method should return:true
if the value is generated by calling a SQL function likecurrent_timestamp
, orfalse
if the value is generated by a trigger, bygenerated always as
, or using a column default value.
- Specified by:
referenceColumnsInSql
in interfaceOnExecutionGenerator
- Returns:
true
if the column is included in the column list of the SQL statement.
-
getReferencedColumnValues
Description copied from interface:OnExecutionGenerator
A SQL expression indicating how to calculate the generated values when the mapped columns are included in the SQL statement. The SQL expressions might be:- function calls like
current_timestamp
ornextval('mysequence')
, or - syntactic markers like
default
.
- Specified by:
getReferencedColumnValues
in interfaceOnExecutionGenerator
- Parameters:
dialect
- The SQL dialect, allowing generation of an expression in dialect-specific SQL.- Returns:
- The column value to be used in the generated SQL statement.
- function calls like
-
getGeneratedIdentifierDelegate
Description copied from interface:OnExecutionGenerator
TheInsertGeneratedIdentifierDelegate
used to retrieve the generated value if this object is an identifier generator.This is ignored by
GeneratedValuesProcessor
, which handles multiple generators at once. So if this object is not an identifier generator, this method is never called.Note that this method arguably breaks the separation of concerns between the generator and coordinating code, by specifying how the generated value should be retrieved.
The problem solved here is: we want to obtain an insert-generated primary key. But, sadly, without already knowing the primary key, there's no completely-generic way to locate the just-inserted row to obtain it.
We need one of the following things:
- a database which supports some form of
insert ... returning
syntax, or can do the same thing using the JDBCgetGeneratedKeys()
API, or - a second unique key of the entity, that is, a property annotated
@NaturalId
.
Alternatively, if the generated id is an identity/"autoincrement" column, we can take advantage of special platform-specific functionality to retrieve it. Taking advantage of the specialness of identity columns is the job of one particular implementation:
IdentityGenerator
. And the need for customized behavior for identity columns is the reason why this layer-breaking method exists.- Specified by:
getGeneratedIdentifierDelegate
in interfaceOnExecutionGenerator
- a database which supports some form of
-