AutoGenerated Primary Keys & Entity EJBs

Discussions

EJB design: AutoGenerated Primary Keys & Entity EJBs

  1. AutoGenerated Primary Keys & Entity EJBs (4 messages)

    Hi,

    I have a yes/no question...

    I have tables in a DB, associated to Entity EJBs, fairly simple 1-to-1 mapping. These tables have triggers that generate the primary keys automatically.

    When I called a BeanHome.create() method, is there any way to fetch the generated primary key into the EJB's associated attribute? Currently, the EJB systematically thinks it has ID=0. I tried the EntityContext.getPrimaryKey(), but it also returns 0.

    I am wondering if I am not breaking the PK concept here, like the EJB and the DB having 2 different PKs for the same physical record.

    Thanks in advance for you answer!

    Christian Faucher
    christian.faucher@tetratec.qc.ca
  2. Are you using BMP or CMP?

    If you are using CMP then you are screwed if you use a trigger to generate your PK. The ejbCreate method will do an insert and then will return the PK object that you passed through. The container will then use the PK object to do a lookup and will return you the populated bean.

    So, if you don't pass through a PK to the ejbCreate method then it will have nothing to return and therefore nothing to lookup.

    If, however, you are using BMP then the world is your Oyster and you can do whatever you want inside your ejbCreate. However, if your DB produces the PK in the trigger, how will you return this to the client??

    Some app servers come with automatic PK generation add-ons that will handle this for you. WebLogic, for instance, works with an Oracle sequence amongst other things. I have used this approach with our Entity Beans (CMP) and it works a treat.

    Good luck,
    Darren.
  3. From an earlyer post on the same topic:

    1. Ugly solution:
    If you can't control the inserting of the primary key yourself, then you could write a finder on this bean which can find it using other unique fields.

    So first do an insert don't bother keeping the returned bean, instead invoke your finder.

    2. Nicer solution:
    If the primary key is generated from a Data Base sequence, then you could get the next sequence number from the DB within the ejbCreate() method.

    eg.
    public Long ejbCreate(...) throws CreateException {
    Long customerID = EJBUtil.getNextSeqNumber("CUSTOMER_SEQ");
    setPK(customerID );
    .
    .
    .
    }

    getNextSeqNumber is a home grown method using JDBC to get the next sequence number from the DB.

    These are the only two options I know of, both of them involve more JDBC being executed then I would like... but thats J2EE.

    Hope that helps,

    Daniel.
  4. Hi,

    Thanks Daniel and Warren for your answers.

    BTW, I use CMP for the EJBs discussed here. Having a separate EJB/Tookit to generate the primary key is my saver for the moment.

    Thanks again!

    Christian Faucher
  5. If you are using BMP you could also use a stored procedure to manage the insertion and return the freshly generated primary key in one step. This will work for just about anything, identity in Sybase, sequence in Oracle, etc. If you use the JDBC escape syntax your code (Java) will also be fairly portable.

    Jay
    jwalters@computer.org