• Entries (RSS)
  • Comments (RSS)

Creating a new Key in WCS

Posted by | Posted in WebSphere Commerce | Posted on 19-11-2008

Tagged Under : , , ,

Creating a new Key in WCS

In WebSphere Commerce, tables that need a unique primary key, the keys are not auto generated. They are coming from the KEYS table. The WCS table KEYS defines the range of key values for all tables that require a unique key. So always new key generated will be using values of KEYS table.

Any time if you need this value programmatically, you can get it using ECKeyManager class. So the code for getting the next keys table for a table is

com.ibm.commerce.key.ECKeyManager keyMgr = com.ibm.commerce.key.ECKeyManager.singleton();
Long key = keyMgr.getNextKey("tablename");

Where tablename is your table’s name. Table name is case sensitive. If your primary key is defined as long data type use getNextKeyAsLong instead of getNextKey method.

In order to work the above code, we need to have an entry in the KEYS table.

Share

Read More

Comments

5 comments posted onCreating a new Key in WCS

  1. Thanks. I was looking for something like this.

  2. Thanks.

  3. Very interesting but just a question: I create the new id for my table and the COUNTER fileld in KEYS table is incremented. If I get an exception in inserting the new row in my table, is COUNTER value rollbacked too?

  4. I tested this scenario and I found that the COUNTER value was not rolled back. It could be because of the way I tested. I used a controller command directly for fetching the data. Immediately after fetching I threw an exception.

  5. I thnk that we’ll implement something outside WCS: we must (it’s an italian law) have a numeric id with no spaces. I mean: 1 -2 -3 it’s ok; 1 – 5 – 6 – 7 it’s ko.

Post a Comment