Class SelectGenerator
- java.lang.Object
-
- org.hibernate.id.SelectGenerator
-
- All Implemented Interfaces:
Serializable
,Generator
,OnExecutionGenerator
,BulkInsertionCapableIdentifierGenerator
,Configurable
,StandardGenerator
,PostInsertIdentifierGenerator
public class SelectGenerator extends Object implements PostInsertIdentifierGenerator, BulkInsertionCapableIdentifierGenerator, StandardGenerator
A generator thatselect
s the just-insert
ed 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:
NaturalId
,UniqueKeySelectingDelegate
, Serialized Form- Implementation Note:
- This also implements the
select
generation type inhbm.xml
mappings.
-
-
Constructor Summary
Constructors Constructor Description SelectGenerator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configure(Type type, Properties parameters, ServiceRegistry serviceRegistry)
Noop default implementation.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-insert
ed row containing the generated value.boolean
referenceColumnsInSql(Dialect dialect)
Determines if the columns whose values are generated are included in the column list of the SQLinsert
orupdate
statement.-
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.BulkInsertionCapableIdentifierGenerator
determineBulkInsertionIdentifierGenerationSelectFragment, supportsBulkInsertionIdentifierGeneration
-
Methods inherited from interface org.hibernate.generator.Generator
generatesOnInsert, generatesOnUpdate, generatesSometimes
-
Methods inherited from interface org.hibernate.generator.OnExecutionGenerator
generatedOnExecution, getGeneratedIdentifierDelegate
-
Methods inherited from interface org.hibernate.id.PostInsertIdentifierGenerator
getEventTypes, writePropertyValue
-
-
-
-
Field Detail
-
KEY
public static final String KEY
The property specifying the unique key name.- See Also:
- Constant Field Values
-
-
Method Detail
-
configure
public void configure(Type type, Properties parameters, ServiceRegistry serviceRegistry)
Description copied from interface:PostInsertIdentifierGenerator
Noop default implementation. May be overridden by subtypes.- Specified by:
configure
in interfaceConfigurable
- Specified by:
configure
in interfacePostInsertIdentifierGenerator
- Parameters:
type
- The id property type descriptorparameters
- param values, keyed by parameter nameserviceRegistry
- Access to service that may be needed.
-
getUniqueKeyPropertyNames
public String[] getUniqueKeyPropertyNames(EntityPersister persister)
Description copied from interface:OnExecutionGenerator
The name of a property of the entity which may be used to locate the just-insert
ed 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
@NaturalId
property, if there is one.- Specified by:
getUniqueKeyPropertyNames
in interfaceOnExecutionGenerator
-
referenceColumnsInSql
public boolean referenceColumnsInSql(Dialect dialect)
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.
-
getReferencedColumnValues
public String[] getReferencedColumnValues(Dialect dialect)
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
-
-