Package org.hibernate.generator
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
Describes the generation of values of a certain field or property of an entity. A generated
value might be generated in Java, or by the database. Every instance of this interface must
implement either or both of
BeforeExecutionGenerator
and OnExecutionGenerator
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
default
column value specified in DDL, for example, or it might be generated by a SQL expression occurring explicitly in the SQLinsert
orupdate
statement. 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,
see generatedOnExecution
and
generatedBeforeExecution
.
Generically, a generator may be integrated with the program using the meta-annotation
ValueGenerationType
, which associates the generator with
a Java annotation type, called the generator 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.
A generator must implement getEventTypes()
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:
-
An identifier 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 theIdGeneratorType
meta-annotation or the older-styleGenericGenerator
annotation. -
A version 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 usingValueGenerationType
meta-annotation.
- Since:
- 6.2
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
Determine if this generator allows identifier values to be manually assigned to the entity instance before persisting it.default boolean
Determine if this generator allows generated fields to be manually assigned a value on events which do not trigger value generation.default boolean
generatedBeforeExecution
(Object entity, SharedSessionContractImplementor session) Determines if the property value is generated before in Java code that executes before the row is written.boolean
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.default boolean
generatedOnExecution
(Object entity, SharedSessionContractImplementor session) Determines if the property value is generated when a row is written to the database.default boolean
default boolean
default boolean
The event 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
BeforeExecutionGenerator
must resultfalse
. - Generators which only implement
OnExecutionGenerator
must resulttrue
. - Generators which implement both subinterfaces may decide at runtime what value to return.
- Returns:
true
if the value is generated by the database as a side effect of the execution of aninsert
orupdate
statement, or false if it is generated in Java code before the statement is executed via JDBC.
- Generators which only implement
-
getEventTypes
The event types for which this generator should be called to produce a new value.Identifier generators must return
EventTypeSets.INSERT_ONLY
.- Returns:
- a set of
EventType
s.
-
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:
true
if this generator allows pre-assigned identifier values,false
otherwise (default).- Since:
- 6.5
-
allowMutation
default boolean allowMutation()Determine if this generator allows generated fields to be manually assigned a value on events which do not trigger value generation.- Returns:
true
if this generator allows manually assigned values,false
otherwise (default).- Since:
- 7.0
-
generatesSometimes
default boolean generatesSometimes() -
generatesOnInsert
default boolean generatesOnInsert() -
generatesOnUpdate
default boolean generatesOnUpdate()
-