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 TypeMethodDescriptionvoidconfigure(GeneratorCreationContext creationContext, Properties parameters) Configure this instance, given the value of parameters specified by the user as XML<param>elements and@Parameterannotations.generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) Generate a value.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.The event types for which this generator should be called to produce a new value.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 are included in the SQL statement.voidinitialize(NativeGenerator annotation, Member member, GeneratorCreationContext context) Initializes this generation strategy for the given annotation instance.voidinitialize(SqlStringGenerationContext context) Initializes this instance, pre-generating SQL if necessary.booleanreferenceColumnsInSql(Dialect dialect) Determines if the columns whose values are generated are included in the column list of the SQLinsertorupdatestatement.voidregisterExportables(Database database) Register the contained exportable things to theDatabasebooleanDetermines 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, waitMethods inherited from interface org.hibernate.id.Configurable
configureMethods inherited from interface org.hibernate.generator.Generator
allowAssignedIdentifiers, allowMutation, generatedBeforeExecution, generatedOnExecution, generatesOnForceIncrement, generatesOnInsert, generatesOnUpdate, generatesSometimesMethods inherited from interface org.hibernate.generator.OnExecutionGenerator
getUniqueKeyPropertyNames
-
Constructor Details
-
NativeGenerator
public NativeGenerator()
-
-
Method Details
-
getGenerationType
-
getEventTypes
Description copied from interface:GeneratorThe event types for which this generator should be called to produce a new value.Identifier generators must return
EventTypeSets.INSERT_ONLY.- Specified by:
getEventTypesin interfaceGenerator- Returns:
- a set of
EventTypes.
-
generatedOnExecution
public 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 interfaceBeforeExecutionGenerator- Specified by:
generatedOnExecutionin interfaceGenerator- Specified by:
generatedOnExecutionin interfaceOnExecutionGenerator- 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
-
initialize
Description copied from interface:AnnotationBasedGeneratorInitializes this generation strategy for the given annotation instance.- Specified by:
initializein 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:ConfigurableConfigure this instance, given the value of parameters specified by the user as XML<param>elements and@Parameterannotations.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:
configurein interfaceConfigurable- Parameters:
creationContext- Access to the generator creation contextparameters- param values, keyed by parameter name
-
registerExportables
Description copied from interface:ExportableProducerRegister the contained exportable things to theDatabase- Specified by:
registerExportablesin interfaceExportableProducer- Parameters:
database- The database instance
-
initialize
Description copied from interface:ConfigurableInitializes 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:
initializein interfaceConfigurable- Parameters:
context- A context to help generate SQL strings
-
referenceColumnsInSql
Description copied from interface:OnExecutionGeneratorDetermines 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 a column default value.
- Specified by:
referenceColumnsInSqlin interfaceOnExecutionGenerator- Returns:
trueif the column is included in the column list of the SQL statement.
-
writePropertyValue
public boolean writePropertyValue()Description copied from interface:OnExecutionGeneratorDetermines if the property values are written to JDBC as the argument of a JDBC?parameter.- Specified by:
writePropertyValuein interfaceOnExecutionGenerator
-
getReferencedColumnValues
Description copied from interface:OnExecutionGeneratorA 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_timestampornextval('mysequence'), or - syntactic markers like
default.
- Specified by:
getReferencedColumnValuesin 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:OnExecutionGeneratorTheInsertGeneratedIdentifierDelegateused 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 ... 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.- Specified by:
getGeneratedIdentifierDelegatein interfaceOnExecutionGenerator
- a database which supports some form of
-