org.hibernate.id.enhanced
Class SequenceStyleGenerator

java.lang.Object
  extended by org.hibernate.id.enhanced.SequenceStyleGenerator
All Implemented Interfaces:
BulkInsertionCapableIdentifierGenerator, Configurable, IdentifierGenerator, PersistentIdentifierGenerator

public class SequenceStyleGenerator
extends Object
implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator, Configurable

Generates identifier values based on an sequence-style database structure. Variations range from actually using a sequence to using a table to mimic a sequence. These variations are encapsulated by the DatabaseStructure interface internally.

NOTE that by default we utilize a single database sequence for all generators. The configuration parameter CONFIG_PREFER_SEQUENCE_PER_ENTITY can be used to create dedicated sequence for each entity based on its name. Sequence suffix can be controlled with CONFIG_SEQUENCE_PER_ENTITY_SUFFIX option.

General configuration parameters:

NAME DEFAULT DESCRIPTION
SEQUENCE_PARAM DEF_SEQUENCE_NAME The name of the sequence/table to use to store/retrieve values
INITIAL_PARAM DEFAULT_INITIAL_VALUE The initial value to be stored for the given segment; the effect in terms of storage varies based on Optimizer and DatabaseStructure
INCREMENT_PARAM DEFAULT_INCREMENT_SIZE The increment size for the underlying segment; the effect in terms of storage varies based on Optimizer and DatabaseStructure
OPT_PARAM depends on defined increment size Allows explicit definition of which optimization strategy to use
FORCE_TBL_PARAM false Allows explicit definition of which optimization strategy to use

Configuration parameters used specifically when the underlying structure is a table:

NAME DEFAULT DESCRIPTION
VALUE_COLUMN_PARAM DEF_VALUE_COLUMN The name of column which holds the sequence value for the given segment


Field Summary
static String CONFIG_PREFER_SEQUENCE_PER_ENTITY
           
static String CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
           
static String DEF_SEQUENCE_NAME
           
static String DEF_SEQUENCE_SUFFIX
           
static String DEF_VALUE_COLUMN
           
static int DEFAULT_INCREMENT_SIZE
           
static int DEFAULT_INITIAL_VALUE
           
static String FORCE_TBL_PARAM
           
static String INCREMENT_PARAM
           
static String INITIAL_PARAM
           
static String OPT_PARAM
           
static String SEQUENCE_PARAM
           
static String VALUE_COLUMN_PARAM
           
 
Fields inherited from interface org.hibernate.id.PersistentIdentifierGenerator
CATALOG, IDENTIFIER_NORMALIZER, PK, SCHEMA, TABLE, TABLES
 
Fields inherited from interface org.hibernate.id.IdentifierGenerator
ENTITY_NAME, JPA_ENTITY_NAME
 
Constructor Summary
SequenceStyleGenerator()
           
 
Method Summary
protected  DatabaseStructure buildDatabaseStructure(Type type, Properties params, Dialect dialect, boolean forceTableUse, String sequenceName, int initialValue, int incrementSize)
          Build the database structure.
 void configure(Type type, Properties params, Dialect dialect)
          Configure this instance, given the value of parameters specified by the user as <param> elements.
protected  int determineAdjustedIncrementSize(String optimizationStrategy, int incrementSize)
          In certain cases we need to adjust the increment size based on the selected optimizer.
 String determineBulkInsertionIdentifierGenerationSelectFragment(Dialect dialect)
          Return the select expression fragment, if any, that generates the identifier values.
protected  int determineIncrementSize(Properties params)
          Determine the increment size to be applied.
protected  int determineInitialValue(Properties params)
          Determine the initial sequence value to use.
protected  String determineOptimizationStrategy(Properties params, int incrementSize)
          Determine the optimizer to use.
protected  String determineSequenceName(Properties params, Dialect dialect)
          Determine the name of the sequence (or table if this resolves to a physical table) to use.
protected  String determineValueColumnName(Properties params, Dialect dialect)
          Determine the name of the column used to store the generator value in the db.
 Serializable generate(SessionImplementor session, Object object)
          Generate a new identifier.
 Object generatorKey()
          Return a key unique to the underlying database objects.
 DatabaseStructure getDatabaseStructure()
          Getter for property 'databaseStructure'.
 Type getIdentifierType()
          Getter for property 'identifierType'.
 Optimizer getOptimizer()
          Getter for property 'optimizer'.
 String[] sqlCreateStrings(Dialect dialect)
          The SQL required to create the underlying database objects.
 String[] sqlDropStrings(Dialect dialect)
          The SQL required to remove the underlying database objects.
 boolean supportsBulkInsertionIdentifierGeneration()
          Given the configuration of this generator, is identifier generation as part of bulk insertion supported?

IMPL NOTE : Mainly here to allow stuff like SequenceStyleGenerator which *can* support this based on configuration

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEQUENCE_PARAM

public static final String SEQUENCE_PARAM
See Also:
Constant Field Values

DEF_SEQUENCE_NAME

public static final String DEF_SEQUENCE_NAME
See Also:
Constant Field Values

INITIAL_PARAM

public static final String INITIAL_PARAM
See Also:
Constant Field Values

DEFAULT_INITIAL_VALUE

public static final int DEFAULT_INITIAL_VALUE
See Also:
Constant Field Values

INCREMENT_PARAM

public static final String INCREMENT_PARAM
See Also:
Constant Field Values

DEFAULT_INCREMENT_SIZE

public static final int DEFAULT_INCREMENT_SIZE
See Also:
Constant Field Values

OPT_PARAM

public static final String OPT_PARAM
See Also:
Constant Field Values

FORCE_TBL_PARAM

public static final String FORCE_TBL_PARAM
See Also:
Constant Field Values

CONFIG_PREFER_SEQUENCE_PER_ENTITY

public static final String CONFIG_PREFER_SEQUENCE_PER_ENTITY
See Also:
Constant Field Values

CONFIG_SEQUENCE_PER_ENTITY_SUFFIX

public static final String CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
See Also:
Constant Field Values

DEF_SEQUENCE_SUFFIX

public static final String DEF_SEQUENCE_SUFFIX
See Also:
Constant Field Values

VALUE_COLUMN_PARAM

public static final String VALUE_COLUMN_PARAM
See Also:
Constant Field Values

DEF_VALUE_COLUMN

public static final String DEF_VALUE_COLUMN
See Also:
Constant Field Values
Constructor Detail

SequenceStyleGenerator

public SequenceStyleGenerator()
Method Detail

getDatabaseStructure

public DatabaseStructure getDatabaseStructure()
Getter for property 'databaseStructure'.

Returns:
Value for property 'databaseStructure'.

getOptimizer

public Optimizer getOptimizer()
Getter for property 'optimizer'.

Returns:
Value for property 'optimizer'.

getIdentifierType

public Type getIdentifierType()
Getter for property 'identifierType'.

Returns:
Value for property 'identifierType'.

configure

public void configure(Type type,
                      Properties params,
                      Dialect dialect)
               throws MappingException
Description copied from interface: Configurable
Configure this instance, given the value of parameters specified by the user as <param> elements. This method is called just once, following instantiation.

Specified by:
configure in interface Configurable
params - param values, keyed by parameter name
Throws:
MappingException

determineSequenceName

protected String determineSequenceName(Properties params,
                                       Dialect dialect)
Determine the name of the sequence (or table if this resolves to a physical table) to use.

Called during configuration.

Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
dialect - The dialect in effect
Returns:
The sequence name

determineValueColumnName

protected String determineValueColumnName(Properties params,
                                          Dialect dialect)
Determine the name of the column used to store the generator value in the db.

Called during configuration when resolving to a physical table.

Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
dialect - The dialect in effect.
Returns:
The value column name

determineInitialValue

protected int determineInitialValue(Properties params)
Determine the initial sequence value to use. This value is used when initializing the database structure (i.e. sequence/table).

Called during configuration.

Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The initial value

determineIncrementSize

protected int determineIncrementSize(Properties params)
Determine the increment size to be applied. The exact implications of this value depends on the optimizer being used.

Called during configuration.

Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The increment size

determineOptimizationStrategy

protected String determineOptimizationStrategy(Properties params,
                                               int incrementSize)
Determine the optimizer to use.

Called during configuration.

Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
incrementSize - The determined increment size
Returns:
The optimizer strategy (name)

determineAdjustedIncrementSize

protected int determineAdjustedIncrementSize(String optimizationStrategy,
                                             int incrementSize)
In certain cases we need to adjust the increment size based on the selected optimizer. This is the hook to achieve that.

Parameters:
optimizationStrategy - The optimizer strategy (name)
incrementSize - The determined increment size
Returns:
The adjusted increment size.

buildDatabaseStructure

protected DatabaseStructure buildDatabaseStructure(Type type,
                                                   Properties params,
                                                   Dialect dialect,
                                                   boolean forceTableUse,
                                                   String sequenceName,
                                                   int initialValue,
                                                   int incrementSize)
Build the database structure.

Parameters:
type - The Hibernate type of the identifier property
params - The params supplied in the generator config (plus some standard useful extras).
dialect - The dialect being used.
forceTableUse - Should a table be used even if the dialect supports sequences?
sequenceName - The name to use for the sequence or table.
initialValue - The initial value.
incrementSize - the increment size to use (after any adjustments).
Returns:
An abstraction for the actual database structure in use (table vs. sequence).

generate

public Serializable generate(SessionImplementor session,
                             Object object)
                      throws HibernateException
Description copied from interface: IdentifierGenerator
Generate a new identifier.

Specified by:
generate in interface IdentifierGenerator
object - the entity or toplevel collection for which the id is being generated
Returns:
a new identifier
Throws:
HibernateException

generatorKey

public Object generatorKey()
Description copied from interface: PersistentIdentifierGenerator
Return a key unique to the underlying database objects. Prevents us from trying to create/remove them multiple times.

Specified by:
generatorKey in interface PersistentIdentifierGenerator
Returns:
Object an identifying key for this generator

sqlCreateStrings

public String[] sqlCreateStrings(Dialect dialect)
                          throws HibernateException
Description copied from interface: PersistentIdentifierGenerator
The SQL required to create the underlying database objects.

Specified by:
sqlCreateStrings in interface PersistentIdentifierGenerator
Parameters:
dialect - The dialect against which to generate the create command(s)
Returns:
The create command(s)
Throws:
HibernateException - problem creating the create command(s)

sqlDropStrings

public String[] sqlDropStrings(Dialect dialect)
                        throws HibernateException
Description copied from interface: PersistentIdentifierGenerator
The SQL required to remove the underlying database objects.

Specified by:
sqlDropStrings in interface PersistentIdentifierGenerator
Parameters:
dialect - The dialect against which to generate the drop command(s)
Returns:
The drop command(s)
Throws:
HibernateException - problem creating the drop command(s)

supportsBulkInsertionIdentifierGeneration

public boolean supportsBulkInsertionIdentifierGeneration()
Description copied from interface: BulkInsertionCapableIdentifierGenerator
Given the configuration of this generator, is identifier generation as part of bulk insertion supported?

IMPL NOTE : Mainly here to allow stuff like SequenceStyleGenerator which *can* support this based on configuration

Specified by:
supportsBulkInsertionIdentifierGeneration in interface BulkInsertionCapableIdentifierGenerator
Returns:
true if bulk insertions are supported; false otherwise.

determineBulkInsertionIdentifierGenerationSelectFragment

public String determineBulkInsertionIdentifierGenerationSelectFragment(Dialect dialect)
Description copied from interface: BulkInsertionCapableIdentifierGenerator
Return the select expression fragment, if any, that generates the identifier values.

Specified by:
determineBulkInsertionIdentifierGenerationSelectFragment in interface BulkInsertionCapableIdentifierGenerator
Parameters:
dialect - The dialect against which the insert will be performed.
Returns:
The identifier value generation fragment (SQL). null indicates that no fragment is needed.


Copyright © 2001-2012 Red Hat, Inc. All Rights Reserved.