Discussions

EJB programming & troubleshooting: Help - Primary Key column is auto increment in the database

  1. I have mapped the primary key for a CMP EJB to a primary key column in the DB2 database. This column is an IDENTITY column (autoincrement). How do I create a new row using the bean. I cant have a create method to which I pass the value of the primary column field as this value will be generated by the database.
    Thanks
  2. This is how we r doing it.
       make another table with only one column that will contain a number which will be incremented everytime it is being called(do this using a stored procedure or something)
    now just before calling the create method on ur entity bean , run a sql querry which will return the value strored in the new table, use this as ur PK of the entity bean.
      The usefullness of doing it like this depends on ur arch. too. we need to call this create method from only one place and that too not very often.
     There might be other ways to do it, but in our project this method is working out fine.
  3. Thanks Somil. I am now doing roughly the same thing you suggested. I am using a Singleton in combination with a table to generate the keys.