Top Description Fields Constructors Methods
org.hibernate.id

public Class SelectGenerator

extends Object
implements PostInsertIdentifierGenerator, BulkInsertionCapableIdentifierGenerator, StandardGenerator
Class Inheritance
All Implemented Interfaces
org.hibernate.id.factory.spi.StandardGenerator, org.hibernate.id.BulkInsertionCapableIdentifierGenerator, org.hibernate.generator.Generator, java.io.Serializable, org.hibernate.id.PostInsertIdentifierGenerator, org.hibernate.id.Configurable, org.hibernate.generator.OnExecutionGenerator
Imports
java.util.Properties, org.hibernate.dialect.Dialect, org.hibernate.generator.OnExecutionGenerator, org.hibernate.id.factory.spi.StandardGenerator, org.hibernate.persister.entity.EntityPersister, org.hibernate.service.ServiceRegistry, org.hibernate.type.Type

A generator that 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 org.hibernate.generator.internal.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 org.hibernate.id.insert.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.

Implementation Note

This also implements the select generation type in hbm.xml mappings.

Author
Gavin King
See Also
org.hibernate.annotations.NaturalId, org.hibernate.id.insert.UniqueKeySelectingDelegate

Field Summary

Modifier and TypeField and Description
public static final String
KEY

The property specifying the unique key name.

private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
configure(Type
The id property type descriptor
type
,
Properties
param values, keyed by parameter name
parameters
,
ServiceRegistry
Access to service that may be needed.
serviceRegistry
)

Overrides default org.hibernate.id.PostInsertIdentifierGenerator.configure.

Noop default implementation.
public String[]
getReferencedColumnValues(Dialect
The SQL dialect, allowing generation of an expression in dialect-specific SQL.
dialect
)

Implements org.hibernate.generator.OnExecutionGenerator.getReferencedColumnValues.

A SQL expression indicating how to calculate the generated values when the mapped columns are included in the SQL statement.
public String[]
getUniqueKeyPropertyNames(EntityPersister persister)

Overrides default org.hibernate.generator.OnExecutionGenerator.getUniqueKeyPropertyNames.

The name of a property of the entity which may be used to locate the just-inserted row containing the generated value.
public boolean
referenceColumnsInSql(Dialect dialect)

Implements org.hibernate.generator.OnExecutionGenerator.referenceColumnsInSql.

Determines if the columns whose values are generated are included in the column list of the SQL insert or update statement.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

KEYback to summary
public static final String KEY

The property specifying the unique key name.

uniqueKeyPropertyNameback to summary
private String uniqueKeyPropertyName

Constructor Detail

SelectGeneratorback to summary
public SelectGenerator()

Method Detail

configureback to summary
public void configure(Type type, Properties parameters, ServiceRegistry serviceRegistry)

Overrides default org.hibernate.id.PostInsertIdentifierGenerator.configure.

Doc from org.hibernate.id.PostInsertIdentifierGenerator.configure.

Noop default implementation. May be overridden by subtypes.

Parameters
type:Type

Doc from org.hibernate.id.Configurable.configure.

The id property type descriptor

parameters:Properties

Doc from org.hibernate.id.Configurable.configure.

param values, keyed by parameter name

serviceRegistry:ServiceRegistry

Doc from org.hibernate.id.Configurable.configure.

Access to service that may be needed.

Annotations
@Override
getReferencedColumnValuesback to summary
public String[] getReferencedColumnValues(Dialect dialect)

Implements org.hibernate.generator.OnExecutionGenerator.getReferencedColumnValues.

Doc from org.hibernate.generator.OnExecutionGenerator.getReferencedColumnValues.

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 or nextval('mysequence'), or
  • syntactic markers like default.
Parameters
dialect:Dialect

The SQL dialect, allowing generation of an expression in dialect-specific SQL.

Returns:String[]

The column value to be used in the generated SQL statement.

Annotations
@Override
getUniqueKeyPropertyNamesback to summary
public String[] getUniqueKeyPropertyNames(EntityPersister persister)

Overrides default org.hibernate.generator.OnExecutionGenerator.getUniqueKeyPropertyNames.

Doc from org.hibernate.generator.OnExecutionGenerator.getUniqueKeyPropertyNames.

The 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 @NaturalId property, if there is one.

Annotations
@Override
referenceColumnsInSqlback to summary
public boolean referenceColumnsInSql(Dialect dialect)

Implements org.hibernate.generator.OnExecutionGenerator.referenceColumnsInSql.

Doc from org.hibernate.generator.OnExecutionGenerator.referenceColumnsInSql.

Determines if the columns whose values are generated are included in the column list of the SQL insert or update statement. For example, this method should return:

Returns:boolean

true if the column is included in the column list of the SQL statement.

Annotations
@Override