EJB programming & troubleshooting: CMP 2.0 entity beans for tables with auto increment primary keys
I'm playing around with JBoss3 on my first EJB prototypes.
- Posted by: davout davout
- Posted on: November 13 2002 13:34 EST
Having built some simple CMP 2.0 entity beans I've found to my horror that CMP doesn't seem to support auto increment primary keys.
I'm wondering if anybody has any solutions to get around this?
I posed this same question on the JBoss site only to get back a cursory "CMP does not support this" response.
Auto increment primary keys are pretty much standard fare in most RDBMS - it seems like a bizarre ommission for CMP2.0 not to support them. By the way I'm using mySQL.
- CMP 2.0 entity beans for tables with auto increment primary keys by Mohit Sehgal on November 14 2002 00:32 EST
- CMP 2.0 entity beans for tables with auto increment primary keys by Amit Gupta on November 18 2002 02:48 EST
I think there are couple of generic ways to handle it. I am not an expert in mysql or jBoss. I mainly use weblogic and oracle (which provides a declarative way to do this in the weblogic specific descriptor).
I do not think ejb specs specify anything about automatic key generation.
One way to handle it is using the object id pattern (works well for surrogate primary key). Actully read this article for real nice explanation. http://castor.exolab.org/key-generator.html
Second way is to define a sequence(it is there in oracle) or a table in your database. For the table approach you can define a row containing 2 columns, one for the entity for which you need to generate the PK, second is the highest PK value. You can do a JDBC call to select and update the PK value (by incrementing it) with in that table. You can also use stored procedure for that.
You need to have autoincrement keys to get the latest entity inserted by the Entity Bean,u can get that by using JDBC 3.0 and JDK 1.4 ???try it out..