Creating primary keys
The ejbCreate
method is used to instantiate new
instances of an entity bean. This method is automatically generated but the
generated method only includes logic to initialize primary keys to a static
value.
About this task
You may need to ensure that the primary key is a new, unique value. In this case, you should have an ejbCreate method similar to the following code snippet:
public YourNewBeanKey ejbCreate(int argMyOtherValue)
throws javax.ejb.CreateException
{
_initLinks();
//Initialize CMP fields.
try {
MyKeyValue = com.ibm.commerce.key.ECKeyManager.
singleton().getNextKey("table_name");
} catch (RemoteException e) {
throw new javax.ejb.EJBException (e) ;
} catch (FinderException e) {
throw new javax.ejb.EJBException (e) ;
} catch (NamingException e) {
throw new javax.ejb.EJBException (e) ;
}
MyOtherValue = argMyOtherValue;
return null;
}
In the preceding code snippet, the getNextKey method generates
a unique integer for the primary key. The table_name
input
parameter for the method must be an exact match to the TABLENAME value that
is defined in the KEYS table. Be certain to match the characters and case
exactly.
In addition to including the preceding code in your ejbCreate method, you must also create an entry in the KEYS table. The following is an example SQL statement to make the entry in the KEYS table:
insert into KEYS (TABLENAME, COUNTER, KEYS_ID)
values ("table_name", 0, 1)
Note that with the preceding SQL statement default values for the other columns in the KEYS table are accepted. The value for COUNTER indicates the value at which the count should start. The value for KEYS_ID should be any positive value.
- BIGINT
- BIGINT
- NUMBER(38, 0))
getNextKeyAsLong
method.Related concepts