Interface OnExecutionGenerator
- All Superinterfaces:
Generator,Serializable
- All Known Subinterfaces:
PostInsertIdentifierGenerator
- All Known Implementing Classes:
AbstractPostInsertGenerator,CurrentTimestampGeneration,GeneratedAlwaysGeneration,GeneratedGeneration,IdentityGenerator,NativeGenerator,SelectGenerator
insert orupdate statement which wrote the row. A value generated by the database might be generated implicitly, by a trigger, or using adefault column value specified in DDL, for example, or it might be generated by a SQL expression occurring explicitly in the SQLinsert orupdate statement.
The generated value is usually retrieved from the database using a SQLselect, but incertain cases this additional round trip may be avoided.
Implementations should overridereferenceColumnsInSql(Dialect),writePropertyValue(), andgetReferencedColumnValues(Dialect) as needed in order to achieve the desired behavior.
In implementation of this interface does not specify how the generated value is retrieved from the database after it is generated, this being the responsibility of the coordinating code inGeneratedValuesProcessor or in an implementation ofInsertGeneratedIdentifierDelegate.
- Since:
- 6.2
Method Summary
Modifier and TypeMethodDescriptiondefault booleanDetermines if the property value is generated when a row is written to the database, or in Java code that executes before the row is written.getGeneratedIdentifierDelegate(EntityPersister persister) TheInsertGeneratedIdentifierDelegateused 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 areincluded in the SQL statement.defaultString[]getUniqueKeyPropertyNames(EntityPersister persister) The name of a property of the entity which may be used to locate the just-inserted row containing the generated value.booleanreferenceColumnsInSql(Dialect dialect) Determines if the columns whose values are generated are included in the column list of the SQLinsertorupdatestatement.booleanDetermines if the property values are written to JDBC as the argument of a JDBC?parameter.Methods inherited from interface org.hibernate.generator.Generator
allowAssignedIdentifiers,allowMutation,generatedBeforeExecution,generatedOnExecution,generatesOnInsert,generatesOnUpdate,generatesSometimes,getEventTypes
Method Details
referenceColumnsInSql
Determines if the columns whose values are generated are included in the column list of the SQLinsertorupdatestatement. For example, this method should return:trueif the value is generated by calling a SQL function likecurrent_timestamp, orfalseif the value is generated by a trigger, bygenerated always as, or using acolumn default value.
- Returns:
trueif the column is included in the column list of the SQL statement.
writePropertyValue
boolean writePropertyValue()Determines if the property values are written to JDBC as the argument of a JDBC?parameter.getReferencedColumnValues
A SQL expression indicating how to calculate the generated values when the mapped columns areincluded in the SQL statement. The SQL expressions might be:- function calls like
current_timestampornextval('mysequence'), or - syntactic markers like
default.
- Parameters:
dialect- TheSQL 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
@Incubatingdefault InsertGeneratedIdentifierDelegate getGeneratedIdentifierDelegate(EntityPersister persister) TheInsertGeneratedIdentifierDelegateused 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 beretrieved.
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 ... returningsyntax, 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.- a database which supports some form of
getUniqueKeyPropertyNames
The name of a property of the entity which may be used to locate the just-inserted row containing the generated value. Of course, the columns mapped by this property should form a unique key of the entity.The default implementation uses the
@NaturalIdproperty, if there is one.generatedOnExecution
default boolean generatedOnExecution()Description copied from interface:GeneratorDetermines if the property value is generated when a row is written to the database, or in Java code that executes before the row is written.- Generators which only implement
BeforeExecutionGeneratormust resultfalse. - Generators which only implement
OnExecutionGeneratormust resulttrue. - Generators which implement both subinterfaces may decide at runtime what value to return.
- Specified by:
generatedOnExecutionin interfaceGenerator- Returns:
trueif the value is generated by the database as a side effect of the execution of aninsertorupdatestatement, or false if it is generated in Java code before the statement is executed via JDBC.
- Generators which only implement