Interface Generator
- All Superinterfaces:
Serializable
- All Known Subinterfaces:
AnnotationBasedGenerator<A>,BeforeExecutionGenerator,BulkInsertionCapableIdentifierGenerator,IdentifierGenerator,OnExecutionGenerator,OptimizableGenerator,PersistentIdentifierGenerator,PostInsertIdentifierGenerator
- All Known Implementing Classes:
AbstractPostInsertGenerator,AbstractUUIDGenerator,Assigned,Assigned,CompositeNestedGeneratedValueGenerator,CurrentTimestampGeneration,ForeignGenerator,GeneratedAlwaysGeneration,GeneratedGeneration,GUIDGenerator,IdentityGenerator,IncrementGenerator,NativeGenerator,OrderedSequenceGenerator,SelectGenerator,SequenceStyleGenerator,SourceGeneration,TableGenerator,TenantIdGeneration,UuidGenerator,UUIDGenerator,UUIDHexGenerator,VersionGeneration
BeforeExecutionGenerator andOnExecutionGenerator depending on whether values are generated in Java code before execution of a SQL statement, or by the database when the SQL statement is executed.- Value generation via arbitrary code written in Java is the responsibility of the method
BeforeExecutionGenerator.generate(SharedSessionContractImplementor, Object, Object, EventType). In this case, the generated value is written to the database just like any other field or property value. The Java code may, of course, ask the database to actually generate the value. Examples include timestamp generation using the JVM system time, and id generation using a database sequence. - A value generated by the database might be generated implicitly, by a trigger, or using a
defaultcolumn value specified in DDL, for example, or it might be generated by a SQL expression occurring explicitly in the SQLinsertorupdatestatement. In this case, the generated value may be retrieved from the database using a SQLselect, though in certain cases this additional round trip may be avoided. An important example is id generation using an identity column.
Generator may implement both interfaces and determine the timing of identifier generation at runtime. Furthermore, this condition can be based on the state of the owner entity, seegeneratedOnExecution andgeneratedBeforeExecution. Generically, a generator may be integrated with the program using the meta-annotationValueGenerationType, which associates the generator with a Java annotation type, called thegenerator annotation. A generator may receive parameters from its generator annotation. The generator may either:
- implement
AnnotationBasedGenerator, and receive the annotation as an argument toAnnotationBasedGenerator.initialize(A, java.lang.reflect.Member, org.hibernate.generator.GeneratorCreationContext), - declare a constructor with the same signature as
AnnotationBasedGenerator.initialize(A, java.lang.reflect.Member, org.hibernate.generator.GeneratorCreationContext), - declare a constructor which accepts just the annotation instance, or
- declare a only default constructor, in which case it will not receive parameters.
AGenerator may be a managed bean (for example, a CDI bean) instantiated by thebean container. In this case, only the first of these options,AnnotationBasedGenerator, is available.
A generator must implementgetEventTypes() to specify the events for which it should be called to produce a new value.EventTypeSets provides a convenient list of possibilities.
There are two especially important applications of this machinery:
- Anidentifier generator is a generator capable of producing surrogate primary key values. An identifier generator must respond to insert events only. That is,
getEventTypes()must returnEventTypeSets.INSERT_ONLY. It may be integrated using theIdGeneratorTypemeta-annotation or the older-styleGenericGeneratorannotation. - Aversion generator is a generator capable of seeding and incrementing version numbers. A version generator must respond to both insert and update events. That is,
getEventTypes()must returnEventTypeSets.INSERT_AND_UPDATE. It may be integrated usingValueGenerationTypemeta-annotation.
- Since:
- 6.2
- See Also:
Method Summary
Modifier and TypeMethodDescriptiondefault booleanDetermine if this generator allows identifier values to be manually assigned to the entity instance before persisting it.default booleanDetermine if this generator allows generated fields to be manually assigned a value onevents which donot trigger value generation.default booleangeneratedBeforeExecution(Object entity,SharedSessionContractImplementor session) Determines if the property value is generated before in Java code that executes before the row is written.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.default booleangeneratedOnExecution(Object entity,SharedSessionContractImplementor session) Determines if the property value is generated when a row is written to the database.default booleandefault booleandefault booleanTheevent types for which this generator should be called to produce a new value.
Method Details
generatedOnExecution
boolean generatedOnExecution()Determines 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.
- 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
generatedOnExecution
Determines if the property value is generated when a row is written to the database.Defaults to
generatedOnExecution(), but may be overridden to allow conditional on-execution value generation based on the current state of the owner entity.- Parameters:
entity- The instance of the entity owning the attribute for which we are generating a value.session- The session from which the request originates.- Returns:
trueif the value is generated by the database as a side effect of the execution of aninsertorupdatestatement.- Since:
- 6.4
- See Also:
generatedBeforeExecution
Determines if the property value is generated before in Java code that executes before the row is written.Defaults to
!generatedOnExecution(), but may be overridden to allow conditional before-execution value generation based on the current state of the owner entity.- Parameters:
entity- The instance of the entity owning the attribute for which we are generating a value.session- The session from which the request originates.- Returns:
trueif the value is generated in Java code before the statement is executed via JDBC.- Since:
- 7.0
- See Also:
getEventTypes
Theevent types for which this generator should be called to produce a new value.Identifier generators must return
EventTypeSets.INSERT_ONLY.- Returns:
- a set of
EventTypes.
allowAssignedIdentifiers
default boolean allowAssignedIdentifiers()Determine if this generator allows identifier values to be manually assigned to the entity instance before persisting it. This is useful when, for example, needing existing assigned values to be used as identifiers and falling back to generated values by default.- Returns:
trueif this generator allows pre-assigned identifier values,falseotherwise (default).- Since:
- 6.5
allowMutation
default boolean allowMutation()Determine if this generator allows generated fields to be manually assigned a value onevents which donot trigger value generation.- Returns:
trueif this generator allows manually assigned values,falseotherwise (default).- Since:
- 7.0
generatesSometimes
default boolean generatesSometimes()generatesOnInsert
default boolean generatesOnInsert()generatesOnUpdate
default boolean generatesOnUpdate()