Skip Headers

# Sequencing With Entity Beans WIth Container-Managed Persistence

Previous

Next

To implement sequencing for entity beans with container-managed persistence, use a sequencing strategy that implements preallocation, such as table sequencing or Oracle native sequencing. Preallocation ensures that the entity bean primary key is available at the `ejbPostCreate` method. If you use non-Oracle native sequencing (for example, Sybase, Microsoft SQL Server, or Informix database native sequencing), be aware that:

- Non-Oracle native sequencing does not strictly conform to any EJB specification, because it does not initialize the primary key for a created object until you commit the transaction that creates the object. EJB specifications prior to 3.0 expect that the primary key is available at `ejbPostCreate` method.

- TopLink CMP integration for IBM WebSphere application server does not support native sequencing other than Oracle native sequencing.

- OC4J and BEA WebLogic Server supports native sequencing; however, this type of native sequencing does not assign or return a primary key for a created object until you commit the transaction in which the object is created. Because of this, if you use native sequencing, commit a transaction immediately after calling the `ejbCreate` method to avoid problems with object identity in the TopLink cache and the container.

TopLink CMP Integration With IBM WebSphere Application Server

The TopLink CMP integration with IBM WebSphere application server does not automatically provide the primary key after calling the `ejbCreate` method. If you deploy to a IBM WebSphere application server, explicitly set the primary key in the `ejbCreate` method. Example: Setting Primary Key in IBM WebSphere illustrates this call in a WebSphere integration.

Setting Primary Key in IBM WebSphere

    public Integer ejbCreate() throws CreateException {
        oracle.toplink.ejb.cmp.was.SessionLookupHelper.getHelper().getSession(this)
          .getActiveUnitofWork().assignSequenceNumber(this);
        return null
    }

This example uses the TopLink `SessionLookupHelper` to look up the correct session and uses the session to assign a sequence number to the bean.

TopLink CMP Integration With OC4J and BEA WebLogic Server

In the TopLink CMP integration with OC4J and BEA WebLogic Server, TopLink automatically sets the primary key field on the bean. You do not pass the key value as a parameter to the `create` method, nor set them in the `create` method. Example: Setting Primary Key in OC4J and BEA WebLogic illustrates this call in a WebLogic integration.

Setting Primary Key in OC4J and BEA WebLogic

    public Integer ejbCreate() throws CreateException {
        return null;
    }

If you are migrating a BEA WebLogic CMP application to OC4J and TopLink persistence (see "Migrating BEA WebLogic Persistence to OC4J TopLink Persistence"), the TopLink migration tool automatically configures your project to use unary table sequencing (see "Unary Table Sequencing") if your application originally used single-column sequence tables in BEA WebLogic.