org.exolab.castor.persist.spi

Interface KeyGenerator

Known Implementing Classes:
HighLowKeyGenerator, IdentityKeyGenerator, MaxKeyGenerator, SequenceKeyGenerator, UUIDKeyGenerator

public interface KeyGenerator

Interface for a key generator. The key generator is used for producing identities for objects before they are created in the database.

All the key generators belonging to the same database share the same non-transactional connection to the database.

The key generator is configured from the mapping file using Bean-like accessor methods.

Version:
$Revision: 1.1.1.1 $ $Date: 2003/03/03 07:08:51 $
Authors:
Assaf Arkin
Oleg Nitz

Field Summary

static byte
AFTER_INSERT
For the key generators of AFTER_INSERT style generateKey(Connection,String,String,Properties) is called after INSERT.
static byte
BEFORE_INSERT
For the key generators of BEFORE_INSERT style generateKey(Connection,String,String,Properties) is called before INSERT.
static byte
DURING_INSERT
For the key generators of DURING_INSERT style generateKey(Connection,String,String,Properties) is never called, all work is done by patchSQL(String,String).

Method Summary

Object
generateKey(Connection conn, String tableName, String primKeyName, Properties props)
Generate a new key for the specified table.
byte
getStyle()
Style of the key generator: BEFORE_INSERT, DURING_INSERT or AFTER_INSERT.
boolean
isInSameConnection()
Is key generated in the same connection as INSERT? For DURING_INSERT style this method is never called.
String
patchSQL(String insert, String primKeyName)
Gives a possibility to patch the Castor-generated SQL statement for INSERT (indended mainly for DURING_INSERT style of key generators, other key generators usually simply return the passed parameter).

Field Details

AFTER_INSERT

public static final byte AFTER_INSERT
Field Value:
1

BEFORE_INSERT

public static final byte BEFORE_INSERT
Field Value:
-1

DURING_INSERT

public static final byte DURING_INSERT
Field Value:
0

Method Details

generateKey

public Object generateKey(Connection conn,
                          String tableName,
                          String primKeyName,
                          Properties props)
            throws PersistenceException
Generate a new key for the specified table. This method is called when a new object is about to be created. In some environments the name of the owner of the object is known, e.g. the principal in a J2EE server. This method is never called for DURING_INSERT key generators.
Parameters:
conn - An open connection within the given transaction
tableName - The table name
primKeyName - The primary key name
props - A temporary replacement for Principal object
Returns:
A new key
Throws:
PersistenceException - An error occured talking to persistent storage

getStyle

public byte getStyle()
Style of the key generator: BEFORE_INSERT, DURING_INSERT or AFTER_INSERT.

isInSameConnection

public boolean isInSameConnection()
Is key generated in the same connection as INSERT? For DURING_INSERT style this method is never called.

patchSQL

public String patchSQL(String insert,
                       String primKeyName)
            throws MappingException
Gives a possibility to patch the Castor-generated SQL statement for INSERT (indended mainly for DURING_INSERT style of key generators, other key generators usually simply return the passed parameter). The original statement contains primary key column on the first place for BEFORE_INSERT style and doesn't contain it for the other styles. This method is called once for each class and must return String with '?' that can be passed to CallableStatement (for DURING_INSERT style) or to PreparedStatement (for the others). Then for each record being created actual field values are substituted, starting from the primary key value for BEFORE_INSERT style, of starting from the first of other fields for the other styles. The DURING_INSERT key generator must add one OUT parameter to the end of the parameter list, which will return the generated identity. For example, ReturningKeyGenerator for Oracle8i transforms "INSERT INTO tbl (pk, fld1, ...,fldN) VALUES (?,?...,?)" to "INSERT INTO tbl (pk, fld1, ...) VALUES (seq.nextval,?....,?) RETURNING pk INTO ?". DURING_INSERT key generator also may be implemented as a stored procedure.
Parameters:
insert - Castor-generated INSERT statement
primKeyName - The primary key name

Intalio Inc. (C) 1999-2003. All rights reserved http://www.intalio.com