If automatic key generation is enabled, keys are created automatically for every row inserted in the table. If you are mapping container-managed fields to multiple tables, automatic key generation applies only to the main table, specified on the Persistence/General subtab.
Specifying the key type for EJB CMP entity beans
There are two options for the primary key type when using automatic key generation in EJB and Java components:
java.lang.Object The EJB 2.0 specification requires this type for entity beans that have automatically generated keys. However, using java.lang.Object makes client coding difficult, particularly if the home interface has finder methods that take key values as input. In this case, you do not know what the actual Java key type is until after deploying the component.
java.lang.Integer or other integer types EAServer allows you to use an integer type with automatic key generation configured. You can also use other integer types, as long as you specify the wrapper class name, such as java.lang.Long.
Specify the key type in the EAServer Manager Component Properties dialog box as follows:
On the Persistence/General subtab, set the Primary Key field to the Java class name used in your code. EAServer Manager saves the setting as the IDL type, and you will see the IDL type after you close and reopen the Component Properties dialog box.
Java key type |
IDL key type |
---|---|
java.lang.Object |
XDT::Integer |
java.lang.Integer |
CtsComponents::LongValue |
Other integral types |
The corresponding CtsComponents value structure |
Regenerate stubs and skeletons for the component if you have changed the primary key type.
Choosing the key generation mechanism
EAServer supports three mechanisms for key generation:
Using the Sybase identity datatype If you are using Sybase Adaptive Server Enterprise or Adaptive Server Anywhere, the main table uses the identity datatype for the primary key. The database manages the creation of new keys. To configure this mechanism, follow the procedure “Configuring key generation to use the Sybase identity datatype”. Alternatively, use a key table as described in “Overriding the use of native identity types”.
Using the Oracle sequence datatype If you are using an Oracle database, the main table uses an Oracle sequence for the primary key. The database manages the creation of new keys. To configure this mechanism, follow the procedure “Configuring key generation using the Oracle sequence datatype”. Alternatively, use a key table as described in “Overriding the use of native identity types”.
Using a key lookup table For any SQL database, you can use a single-row, single-integer-column table to generate key values. EAServer increments the key lookup value to generate new keys. If other processes or applications insert to the table, they must also use the key lookup table. To configure this mechanism, follow the procedure “Configuring key generation to use a key lookup table”.
Configuring key generation to use the Sybase identity datatype
Use the Advanced tab to set the property com.sybase.jaguar.component.generateKey to true
.
The default is false
.
In the Persistence/Field Mappings tab, verify that the key field is mapped to the Sybase identity type or a compatible type.
Use the Advanced tab to verify that the property com.sybase.jaguar.component.db.sequence is not set, or set to no value.
Configuring key generation using the Oracle sequence datatype
Use the Advanced tab to set the property com.sybase.jaguar.component.generateKey to true
.
The default is false
.
In the Persistence/Field Mappings tab, verify that the key field is mapped to the Oracle sequence type or a compatible type.
If using an existing table or a table that you create yourself (rather than relying on autocreation by EAServer), use the Advanced tab to set the component property com.sybase.jaguar.component.db.sequence. This property specifies the name of the Oracle sequence to use. The default is:
table_keys
Where table is the main table name specified on the Persistence/General subtab.
Configuring key generation to use a key lookup table
Use the Advanced tab to set the property com.sybase.jaguar.component.generateKey to true
.
The default is false
.
In the Persistence/Field Mappings tab, verify that the key field is mapped to an integer type that is compatible with the type used in the lookup table–see “Setting field-mapping properties”.
Use the Advanced tab to set the component property com.sybase.jaguar.component.db.sequence to a value:
table.column += key_use_rate
or
table += key_use_rate
Where:
table is
the name of the key lookup table. The table must contain a single
row with a single integer column. The column datatype cannot have
precision greater than 64 bits. The default key lookup table name is
the main table name appended with _keys
;
for example, if the main table is phone, the
key lookup table is phone_keys.
column is the column name, which must be an integer column or another integral datatype. If you specify only a table name, the table must contain a column named next_key.
key_use_rate is the number of keys that are reserved at once. To prevent different threads from creating duplicate keys, EAServer uses a semaphore to synchronize the key increment operation. Each thread reserves key_use_rate key values per increment. The key use rate can be tuned to reduce inter-thread contention for locks on the key table. The default of 100 results in good performance for most applications. Very large values can result in large gaps between key values. Gaps in the key sequence are possible if the key use rate is greater than 1.
Create the key lookup table if it does not exist in the database.
Overriding the use of native identity types
If you are using Sybase or Oracle, you can force the use of a key lookup table rather than the Sybase identity or Oracle sequence datatypes. To do so, set the com.sybase.jaguar.component.db.sequence. property and specify a key use rate as described in “Configuring key generation to use a key lookup table”. If the value of this property contains “+=”, EAServer uses a key lookup table regardless of the database type.
Copyright © 2005. Sybase Inc. All rights reserved. |