CMP 2.0 entity beans for tables with auto increment primary keys


EJB programming & troubleshooting: CMP 2.0 entity beans for tables with auto increment primary keys

  1. I'm playing around with JBoss3 on my first EJB prototypes.

    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.
  2. 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.

    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.
  3. 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..