Primary Key


EJB design: Primary Key

  1. Primary Key (1 messages)

     Iam using BMP entity ,my table has identity column ,i want to get the primary key value once i insert a row in the databse,should i call select on the table once row is inserted or is there any other diffrent way to get the recently inserted rows primary key?Also if i call select immediatly after insert can i be sure that iam getting the id of the record i have inserted ?

    Threaded Messages (1)

  2. Primary Key[ Go to top ]

    You can't be sure - because you don't have the PK of the row... how will you select it? :)

    The new JDBC spec (3.0) has a feature that let's you retrieve auto-generated values from the DB. You can see it in section 13.6.
    If you can't get a compliant 3.0 JDBC driver, the Patterns section has a couple of solutions you can use to generate keys yourself. None of them is as good as using the DB to generate them (IMHO), but there aren't too many other options.
    One more option you can use is to have a stored procedure in the DB that does the inserting, retrives the auto-generated values, and returns them as out params to the caller. Then you use a CallableStatement to call this procedure from your code. BTW, this can increase performance.