views:

22

answers:

1

Hello Community, is it possible to map bean's key field with identity primary key column in DB2? Sample table: CREATE TABLE ADDRESS ( ID INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 NO CYCLE CACHE 20 NO ORDER ), Line1 VARCHAR(255) NOT NULL, Line2 VARCHAR(255), City VARCHAR(255) NOT NULL, Postcode VARCHAR(6) NOT NULL, Country VARCHAR(50) NOT NULL, Latitude DOUBLE, Longitude DOUBLE ) AUDIT NONE DATA CAPTURE NONE CCSID UNICODE;

ejbCreate methods have been tailored NOT TO set ID field, but it gets initialized with default for integer type - 0 so i'm getting DuplicateKeyException on second and following calls to ejbCreate. What is the best way to implement IDENTITY behavior? I found many examples for JBoss but nothing for WAS. It was easy with JPA, but CMP 2.0 is a must at this time

A: 

Override method ejbPostCreate. You will be able to retrieve the generated ID from there, and update your model and your code in order to avoid duplicate IDs.

For instance, take a look at http://forums.sun.com/thread.jspa?threadID=699131

Sebastian
I implemented additional "sequence" bean instead described in Duke's bank application (Sun J2EE 1.4 Tutorial). Thanks for advice, I'll try overriding ejbPostCreate another time
grzegorz_p