Class SelectGenerator
- All Implemented Interfaces:
Serializable,Generator,OnExecutionGenerator,BulkInsertionCapableIdentifierGenerator,Configurable,PostInsertIdentifierGenerator
selects the just-inserted row to determine the
column value assigned by the database. The correct row is located using a unique
key of the entity, either:
The second approach is provided for backward compatibility with older versions of Hibernate.
This generator is intended for use with primary keys assigned by a database trigger or something similar, for example:
@Entity @Table(name="TableWithPKAssignedByTrigger")
@GenericGenerator(name = "triggered", type = SelectGenerator.class)
public class TriggeredEntity {
@Id @GeneratedValue(generator = "triggered")
private Long id;
@NaturalId
private String name;
...
}
However, after a very long working life, this generator is now handing over its
work to GeneratedGeneration, and the
above code may be written as:
@Entity @Table(name="TableWithPKAssignedByTrigger")
public class TriggeredEntity {
@Id @Generated
private Long id;
@NaturalId
private String name;
...
}
For tables with identity/autoincrement columns, use IdentityGenerator.
The actual work involved in retrieving the primary key value is the job of
UniqueKeySelectingDelegate.
Arguably, this class breaks the natural separation of responsibility between the generator and the coordinating code, since its role is to specify how the generated value is retrieved.
- See Also:
- Implementation Note:
- This also implements the
selectgeneration type inhbm.xmlmappings.
-
Field Summary
Fields -
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.String[]getReferencedColumnValues(Dialect dialect) A SQL expression indicating how to calculate the generated values when the mapped columns are included in the SQL statement.String[]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.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.hibernate.id.BulkInsertionCapableIdentifierGenerator
determineBulkInsertionIdentifierGenerationSelectFragment, supportsBulkInsertionIdentifierGenerationMethods inherited from interface org.hibernate.id.Configurable
initializeMethods inherited from interface org.hibernate.generator.Generator
allowAssignedIdentifiers, allowMutation, generatedBeforeExecution, generatedOnExecution, generatesOnForceIncrement, generatesOnInsert, generatesOnUpdate, generatesSometimesMethods inherited from interface org.hibernate.generator.OnExecutionGenerator
generatedOnExecution, getGeneratedIdentifierDelegateMethods inherited from interface org.hibernate.id.PostInsertIdentifierGenerator
configure, getEventTypes, writePropertyValue
-
Field Details
-
KEY
The property specifying the unique key name.- See Also:
-
-
Constructor Details
-
SelectGenerator
public SelectGenerator()
-
-
Method Details
-
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
-
getUniqueKeyPropertyNames
Description copied from interface:OnExecutionGeneratorThe 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.- Specified by:
getUniqueKeyPropertyNamesin interfaceOnExecutionGenerator
-
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.
-
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
-