how to generate the unique primary key for entity bean

Discussions

EJB programming & troubleshooting: how to generate the unique primary key for entity bean

  1. how to generate the unique primary key for entity bean (5 messages)

    hello
    i am the beginner to entity bean,and usually use the toplink as the o-r mapping tool.it can generate the unique primary key automatically for me,i don't need to worry about it.
    now i transfer to entity bean,i found that i must generate the pk in my code(if i use the integer id as primary key),then invoke the "create" method to insert a record to database,but how can i keep the id unique?
    who can introduce me the best mechanism to generate pk for the entity bean?

    thank you very much!

    Threaded Messages (5)

  2. for autogenerate the primary key for an entity bean, I write a method into a session bean, like that public Long giveId(String sequenceName, String databaseDatasource) ). This body of this method is
    con = this.getConnection( databaseDatasource );
     stmt = con.createStatement();
       res = stmt.executeQuery("SELECT " + sequenceName + ".nextval from DUAL");
         while( res.next() )
           id = res.getLong(1);
       res.close();
      stmt.close();
    con.close(); As you can see i call the query SELECT nameSequence.nextval FROM DUAL. An the method getConnection is a private method who will return a connection from the datasource with name databaseDatasource. The body of that method is as follow:
    Connection con = null;

    Throwable th = null;

    try
    {
    Hashtable h = new Hashtable();
    h.put(Context.INITIAL_CONTEXT_FACTORY,
    "weblogic.jndi.WLInitialContextFactory");
    h.put(Context.PROVIDER_URL, "t3://localhost:7001");

    InitialContext ctx = new InitialContext(h);

    DataSource ds = (DataSource)ctx.lookup( databaseDatasource );

    con = ds.getConnection();

                h = null; ctx= null; ds = null;

    }
    catch( Exception ex)
    {
    th = ex;
    }

    if (th != null)
    throw new IdException(th);

    return con;

    When you will call the method giveId the sequencename you write there is the name for the sequence you want to increase the value and the databaseDatasource you have to write there is the name of the datasource you use to configure your application server
  3. You don't mention which J2ee server you are using.

    Jboss can automatically create a ID for you using several different methods including using a class you specify. Look for <entity>
    check here:
    http://www.mail-archive.com/jboss-user at lists dot sourceforge dot net/msg29049.html

    WebLogic 6.1 can use something like

    <automatic-key-generation>
    <generator-type>NAMED_SEQUENCE_TABLE</generator-type>
    <generator-name>CUSTOMER_SEQUENCE_TABLE</generator-name>
    <key-cache-size>10</key-cache-size>
    </automatic-key-generation>
    Look here: http://rickhightower.blogspot.com/2002_09_01_rickhightower_archive.html


    etc.. .WG
  4. The other way is[ Go to top ]

    Hi, Jboss gives a functionality to create auto Primary key, but what if you need to create the primary key with some customize formulla..

    you can do this within the session bean or define a method in entity bean home interface if you are only dealing with entity bean.

    you dont' need to call create first, you can simply get the refference of remote interface by calling let say this sample code present..

    when dealing with only entity bean no session bean

    AccountHome home=(AccountHome)PortableRemoteObject.narrow(obj,AccountHome.class);
    int id or String id=home.getNextPrimaryKey();

    remeber "getNextPrimaryKey() is the method which is defined in you entity home interface reason for defining it into the home interface rather then remote interface is that this method is not specifc to any given data (row) the benefit of defining this method inside the entity home interface is that they are called from a bean in the pool before the bean is associated with any given data.

    if you are also using session bean in your application then do the same thing from session bean, because it save the extra remote call.

    regards...
    Raheel
  5. Thread[ Go to top ]

    This thread on primary key generation appeared about three years ago, and may still be of interest:

    http://theserverside.com/patterns/thread.jsp?thread_id=220
  6. Thread[ Go to top ]

    thanks for all of your helps!