Class NativeGenerator
- All Implemented Interfaces:
Serializable
,ExportableProducer
,AnnotationBasedGenerator<NativeGenerator>
,BeforeExecutionGenerator
,Generator
,OnExecutionGenerator
,Configurable
- Since:
- 7.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
configure
(GeneratorCreationContext creationContext, Properties parameters) Configure this instance, given the value of parameters specified by the user as XML<param>
elements and@Parameter
annotations.generate
(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) Generate a value.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.The event types for which this generator should be called to produce a new value.getGeneratedIdentifierDelegate
(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.void
initialize
(NativeGenerator annotation, Member member, GeneratorCreationContext context) Initializes this generation strategy for the given annotation instance.void
initialize
(SqlStringGenerationContext context) Initializes this instance, pre-generating SQL if necessary.boolean
referenceColumnsInSql
(Dialect dialect) Determines if the columns whose values are generated are included in the column list of the SQLinsert
orupdate
statement.void
registerExportables
(Database database) Register the contained exportable things to theDatabase
boolean
Determines if the property values are written to JDBC as the argument of a JDBC?
parameter.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.Configurable
configure
Methods inherited from interface org.hibernate.generator.Generator
allowAssignedIdentifiers, allowMutation, generatedBeforeExecution, generatedOnExecution, generatesOnInsert, generatesOnUpdate, generatesSometimes
Methods inherited from interface org.hibernate.generator.OnExecutionGenerator
getUniqueKeyPropertyNames
-
Constructor Details
-
NativeGenerator
public NativeGenerator()
-
-
Method Details
-
getGenerationType
-
getEventTypes
Description copied from interface:Generator
The event types for which this generator should be called to produce a new value.Identifier generators must return
EventTypeSets.INSERT_ONLY
.- Specified by:
getEventTypes
in interfaceGenerator
- Returns:
- a set of
EventType
s.
-
generatedOnExecution
public boolean generatedOnExecution()Description copied from interface:Generator
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.
- Specified by:
generatedOnExecution
in interfaceBeforeExecutionGenerator
- Specified by:
generatedOnExecution
in interfaceGenerator
- Specified by:
generatedOnExecution
in interfaceOnExecutionGenerator
- 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
-
initialize
Description copied from interface:AnnotationBasedGenerator
Initializes this generation strategy for the given annotation instance.- Specified by:
initialize
in interfaceAnnotationBasedGenerator<NativeGenerator>
- Parameters:
annotation
- an instance of the strategy's annotation type. Typically, implementations will retrieve the annotation's attribute values and store them in fields.member
- the Java member annotated with the generator annotation.context
- aGeneratorCreationContext
-
configure
Description copied from interface:Configurable
Configure this instance, given the value of parameters specified by the user as XML<param>
elements and@Parameter
annotations.This method is called just once, following instantiation. If this instance also implements
ExportableProducer
, then this method is always called beforeExportableProducer.registerExportables(Database)
,- Specified by:
configure
in interfaceConfigurable
- Parameters:
creationContext
- Access to the generator creation contextparameters
- param values, keyed by parameter name
-
registerExportables
Description copied from interface:ExportableProducer
Register the contained exportable things to theDatabase
- Specified by:
registerExportables
in interfaceExportableProducer
- Parameters:
database
- The database instance
-
initialize
Description copied from interface:Configurable
Initializes this instance, pre-generating SQL if necessary.If this instance also implements
ExportableProducer
, then this method is always called afterExportableProducer.registerExportables(Database)
, and before first use.- Specified by:
initialize
in interfaceConfigurable
- Parameters:
context
- A context to help generate SQL strings
-
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.
-
writePropertyValue
public boolean writePropertyValue()Description copied from interface:OnExecutionGenerator
Determines if the property values are written to JDBC as the argument of a JDBC?
parameter.- Specified by:
writePropertyValue
in interfaceOnExecutionGenerator
-
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
-