org.exolab.castor.persist.spi

Interface KeyGenerator

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: 5951 $ $Date: 2005-04-25 15:33:21 -0600 (Mon, 25 Apr 2005) $

Author: Assaf Arkin Oleg Nitz Bruce Snyder

Field Summary
static byteAFTER_INSERT
For the key generators of AFTER_INSERT style KeyGenerator is called after INSERT. KeyGenerator may be used but usually doesn't.
static byteBEFORE_INSERT
For the key generators of BEFORE_INSERT style KeyGenerator is called before INSERT. KeyGenerator may be used but usually doesn't.
static byteDURING_INSERT
For the key generators of DURING_INSERT style KeyGenerator is never called, all work is done by KeyGenerator.
Method Summary
ObjectgenerateKey(Connection conn, String tableName, String primKeyName, Properties props)
Generate a new key for the specified table.
bytegetStyle()
Style of the key generator: BEFORE_INSERT, DURING_INSERT or AFTER_INSERT.
booleanisInSameConnection()
Is key generated in the same connection as INSERT?
StringpatchSQL(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).
voidsupportsSqlType(int sqlType)
Determine if the key generator supports a given sql type.

Field Detail

AFTER_INSERT

public static final byte AFTER_INSERT
For the key generators of AFTER_INSERT style KeyGenerator is called after INSERT. KeyGenerator may be used but usually doesn't.

BEFORE_INSERT

public static final byte BEFORE_INSERT
For the key generators of BEFORE_INSERT style KeyGenerator is called before INSERT. KeyGenerator may be used but usually doesn't.

DURING_INSERT

public static final byte DURING_INSERT
For the key generators of DURING_INSERT style KeyGenerator is never called, all work is done by KeyGenerator.

Method Detail

generateKey

public Object generateKey(Connection conn, String tableName, String primKeyName, Properties props)
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)
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

supportsSqlType

public void supportsSqlType(int sqlType)
Determine if the key generator supports a given sql type.

Parameters: sqlType

Throws: MappingException

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