Saturday 24 September 2011

Creating System Generated primary keys in WCS Entity Beans

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. You may need to ensure that the primary key is a new, unique value. In this case, you may have an ejbCreate method similar to the following code snippet:

public void ejbCreate(int argMyOtherValue) throws javax.ejb.CreateException {
//Initialize CMP fields MyKeyValue = com.ibm.commerce.key.ECKeyManager. singleton().getNextKey("table_name"); 
MyOtherValue = argMyOtherValue; 
}
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. If your primary key is defined as a long data type (BIGINT for DB2 or NUMBER(38, 0) for Oracle), use the getNextKeyAsLong method.

No comments:

Post a Comment

How to customize java.util.logging.Logger class to write logs in separate file than System.out.log in Websphere commerce/ HCL commerce)

/** * This method updated the passed in java.util.logging.Logger object with * custom file handler to write logs data form that class ...