Entity bean primary key generation

Discussions

EJB programming & troubleshooting: Entity bean primary key generation

  1. Entity bean primary key generation (2 messages)

    I am using CMP for Entity Beans.

    Say I have three attributes in my bean called Book namely "bookID", "bookName", "author"
    each of which map to a database column in a table called tblbook.

    There is a create method in my home interface that accepts all above three attributes as arguments like this

    Book create(String bookID, String bookName, String author ) throws CreateException, RemoteException;

    For this I have a corresponding ejbCreate in my Bean class and all the set/get methods are abstract in this class

    String ejbCreate(String bookID, String bookName, String author ) throws CreateException, RemoteException
    {
        setBookID(bookID);
        setBookName(bookName);
        setAuthor(author);

        return bookID;
    }

    I know the container has the job of making a new database entry everytime I call create from my home by providing some kind of implementation to these abstract methods.

    My problem is this. I want to pass only two parameters in my create i.e. I want to pass only bookName and author.

    I want bookID be automatically generated as a unique number using an oracle sequence. Since my set method is abstract I don't know exactly what to do.

    I also want to avoid 2 database hits. I will be calling the entity bean from a session bean. I dont want the entity bean to call back a session bean again just to retrieve a unique ID from the DB.

    How do I create an instance of my entity bean with an automatically generated bookID?
  2. Entity bean primary key generation[ Go to top ]

    A lot of containers now have the ability for Entity beans to retrieve their primary key from a sequence.

    For instance Weblogic 6.1,

    http://e-docs.bea.com/wls/docs61/ejb/cmp.html#1066165

    If your container does not support this there is nothing stopping you from making a sql call from within the Entity bean,

    David
  3. I have used two different methods quite successfully.

    1. Generating a key in the code using a GUID generator to use as a key.
    2. Using the sequence generator session bean supplied as part of the JBOSS 3.0 template wich can be downloaded from

    http://prdownloads.sourceforge.net/jboss/JBoss.3.0TemplateAndExamples.zip

    The sequencegenerator bean is quite simple it has essentially one method called getNextNumber(String pSequenceName), it maintains a table with and entry for each Sequence you want to maintain the method merely gets the current number from the table adds 1 and returns the value to you.

    The main differences between the two are the GUID method does not create a sequence (assuming you care) it is essentially a random distribution but it has the advantage of not requiring any database access.

    I did make a simple change to the template code and modfied it to use a local versus remote interface because it is alot faster.