org.castor.cpa.persistence.sql.keygen
Class HighLowKeyGenerator

java.lang.Object
  extended by org.castor.cpa.persistence.sql.keygen.HighLowKeyGenerator
All Implemented Interfaces:
KeyGenerator

public final class HighLowKeyGenerator
extends java.lang.Object
implements KeyGenerator

HIGH-LOW key generators.

Version:
$Revision: 7982 $ $Date: 2006-04-10 16:39:24 -0600 (Mon, 10 Apr 2006) $
Author:
Oleg Nitz, Bruce Snyder, Ralf Joachim
See Also:
HighLowKeyGeneratorFactory

Field Summary
 
Fields inherited from interface org.exolab.castor.persist.spi.KeyGenerator
AFTER_INSERT, BEFORE_INSERT, DURING_INSERT
 
Constructor Summary
HighLowKeyGenerator(PersistenceFactory factory, java.util.Properties params, int sqlType)
          Initialize the HIGH-LOW key generator.
 
Method Summary
 java.lang.Object generateKey(java.sql.Connection conn, java.lang.String tableName, java.lang.String primKeyName, java.util.Properties props)
          Generate a new key for the specified table.
 byte getStyle()
          Style of the key generator: BEFORE_INSERT, DURING_INSERT or AFTER_INSERT.
 void initFromParameters(java.util.Properties params)
           
 boolean isInSameConnection()
          Is key generated in the same connection as INSERT? For DURING_INSERT style this method is never called.
 java.lang.String patchSQL(java.lang.String insert, java.lang.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).
 void supportsSqlType(int sqlType)
          Determine if the key generator supports a given sql type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HighLowKeyGenerator

public HighLowKeyGenerator(PersistenceFactory factory,
                           java.util.Properties params,
                           int sqlType)
                    throws MappingException
Initialize the HIGH-LOW key generator.

Throws:
MappingException
Method Detail

initFromParameters

public void initFromParameters(java.util.Properties params)
                        throws MappingException
Throws:
MappingException

generateKey

public java.lang.Object generateKey(java.sql.Connection conn,
                                    java.lang.String tableName,
                                    java.lang.String primKeyName,
                                    java.util.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.

Specified by:
generateKey in interface KeyGenerator
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

supportsSqlType

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

Specified by:
supportsSqlType in interface KeyGenerator
Throws:
MappingException

getStyle

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

Specified by:
getStyle in interface KeyGenerator

isInSameConnection

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

Specified by:
isInSameConnection in interface KeyGenerator

patchSQL

public java.lang.String patchSQL(java.lang.String insert,
                                 java.lang.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.

Specified by:
patchSQL in interface KeyGenerator
Parameters:
insert - Castor-generated INSERT statement
primKeyName - The primary key name


Copyright © 2011. All Rights Reserved.