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: 1.1.1.1 $ $Date: 2003/03/03 07:08:51 $
Author: Assaf Arkin Oleg Nitz
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?
|
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).
|
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.
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.
public static final byte DURING_INSERT
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
public byte getStyle()
Style of the key generator: BEFORE_INSERT, DURING_INSERT or AFTER_INSERT.
public boolean isInSameConnection()
Is key generated in the same connection as INSERT?
For DURING_INSERT style this method is never called.
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
Intalio Inc. (C) 1999-2003. All rights reserved http://www.intalio.com