Use of sequence key in Entity beans


EJB programming & troubleshooting: Use of sequence key in Entity beans

  1. Use of sequence key in Entity beans (2 messages)

    One of the columns in the table is a primary key which
    is auto generated by the MSSQL database using the
    IDENTITY qualifier. I want some code snippet
    to use the same feature in my Entity Bean and return
    the primary key (using entityContext.getPrimaryKey()).
    Which means I want to use primary key as the autogenerated
    value in my entity bean and have that value returned
    when i call entityContext.getPrimaryKey().

    Has anybody tried this? Is this doable at all?

  2. Use of sequence key in Entity beans[ Go to top ]

    CMP: I'm not sure how you'd do this. The application server queries your fields that you set up during ejbCreate() and uses those fields to populate the database.

    BMP: You should be able to get away with it by asking the database to generate the ID during the SQL you write in ejbCreate().
  3. I'm using CMP, and tried to do what you're suggesting, but had no luck.

    If you used BMP, you could run the SQL "select @@identity" which is a mssql specific call to find the indenity of the last record inserted.

    What I did was made a EJB entity bean called PrimaryKeyManager, and it has 1 field for each entity bean. For example it has a field called _productCurrentPrimaryKey, and a method like this:

    int getNextProductPrimaryKey()
    return _productCurrentPrimarykey;

    - Alex