Previous Next vertical dots separating previous/next from contents/index/pdf

Automatic Primary Key Generation

With Workshop for WebLogic you can specify in your bean code that a primary key should automatically be generated when creating a new CMP entity bean. This eliminates the need for you to provide primary key values. You can auto-generate primary keys in various vendor-specific ways — using Oracle, SQLServer, or SQLServer2000 — or you can use a vendor-neutral named sequence table. In all cases auto-generated primary keys are of type Integer or Long.

The topics in this section are:


Primary Key Generation Using Oracle's Sequence

Oracle provides the sequence utility to automatically generate unique primary keys. To use this utility to auto-generate primary keys for a CMP entity bean, you must create a sequence table and use the @AutomaticKeyGeneration annotation to point to this table.

In your Oracle database, you must create a sequence table that will create the primary keys, as shown in the following example:

create sequence myOracleSequence
start with 1 
nomaxvalue; 

This creates a sequences of primary key values, starting with 1, followed by 2, 3, and so forth. The sequence table in the example uses the default increment 1, but you can change this by specifying the increment keyword, such as increment by 3. When you do the latter, you must specify the exact same value in the cacheSize attribute of the @AutomaticKeyGeneration annotation:

@AutomaticKeyGeneration(cacheSize = "3", 
        name = "myOracleSequence", 
        type = AutomaticKeyGeneration.AutomaticKeyGenerationType.SEQUENCE)

If you have specified automatic table creation in the CMP bean's project settings, the sequence table will be created automatically when the entity bean is deployed. For more information, see @JarSettings Annotation. For more information on the definition of a CMP entity bean, see below.

Primary Key Generation Using SQL Server's IDENTITY

In SQL Server you can use the IDENTITY keyword to indicate that a primary-key needs to be auto-generated. The following example shows a common scenario where the first primary key value is 1, and the increment is 1:

CREATE TABLE Customer (Customer_ID int IDENTITY(1,1), FirstName varchar(30) LastName varchar(30))

In the CMP entity bean definition you need to specify SQLServer(2000) as the type of automatic key generator you are using. You can also provide a cache size:

@AutomaticKeyGeneration(cacheSize = "3", 
        name = "mySqlServerID", 
        type = AutomaticKeyGeneration.AutomaticKeyGenerationType.IDENTITY)

If you have specified automatic table creation in the CMP bean's project settings, the sequence table will be created automatically when the entity bean is deployed. For more information, see @JarSettings Annotation. For more information on the definition of a CMP entity bean, see below.

Primary Key Generation Using a Named Sequence Table

A named sequence table is similar to the Oracle sequence functionality in that a dedicated table is used to generate primary keys. However, the named sequence table approach is vendor-neutral. To auto-generate primary keys this way, create a named sequence table using the two SQL statements shown in the example:

CREATE Table MyNamedSequence (SEQUENCE number);

INSERT into MyNamedSequence VALUES (0);

In the CMP entity bean definition you need to specify the named sequence table as the type of automatic key generator you are using. You can also provide a cache size:

@AutomaticKeyGeneration(cacheSize = "100", 
        name = "MyNamedSequence", 
        type = AutomaticKeyGeneration.AutomaticKeyGenerationType.SEQUENCE_TABLE)

If you have specified automatic table creation in the CMP bean's project settings, the sequence table will be created automatically when the entity bean is deployed. For more information, see @JarSettings Annotation. For more information on the definition of a CMP entity bean, see the next section.

Note. When you specify a cacheSize value for a named sequence table, a series of unique values are reserved for entity bean creation. When a new cache is necessary, a second series of unique values is reserved, under the assumption that the first series of unique values was entirely used. This guarantees that primary key values are always unique, although it leaves open the possibility that primary key values are not necessarily sequential. For instance, when the first series of values is 10...20, the second series of values is 21-30, even if not all values in the first series were actually used to create entity beans.

Defining the CMP Entity Bean

When defining a CMP entity bean that uses one of the primary key generators, you use the the @AutomaticKeyGeneration annotation to point to the name of the primary key generator table to obtain primary keys. Also, you must define a primary key field of type Integer or Long to set and get the auto-generated primary key. However, the ejbCreate method does not take a primary key value as an argument. Instead the EJB container adds the correct primary key to the entity bean record.

The following example shows what the entity bean might look like. Notice that the bean uses the named sequence option described above, and that ejbCreate method does not take a primary key:
@AutomaticKeyGeneration(cacheSize = "1", 
        name = "NamedSequence", 
        type = AutomaticKeyGeneration.AutomaticKeyGenerationType.SEQUENCE_TABLE)
@Entity(defaultTransaction = Constants.TransactionAttribute.SUPPORTS, 
        primKeyClass = "Integer", 
        ejbName = "Customer_APK", 
        dataSourceName = "samplesDataSource", 
        tableName = "ejb_customer", 
        abstractSchemaName = "Customer")
@FileGeneration(localClass = Constants.Bool.TRUE, 
        localClassName = "AutomaticPK_CustomerLocal", 
        localHome = Constants.Bool.TRUE, 
        localHomeName = "AutomaticPK_CustomerHome", 
        remoteClass = Constants.Bool.FALSE, 
        remoteHome = Constants.Bool.FALSE, 
        remoteHomeName = "CustomerRemoteHome", 
        remoteClassName = "CustomerRemote", 
        valueClass = Constants.Bool.FALSE, 
        valueClassName = "CustomerValue", 
        pkClass = Constants.Bool.TRUE)
@JndiName(local = "ejb.AutomaticPK_CustomerLocalHome")
public abstract class Customer_APK 
    extends GenericEntityBean implements EntityBean {

    @CmpField(column = "FirstName")
    @LocalMethod()
    public abstract String getFirstName();

    @LocalMethod()
    public abstract void setFirstName(String arg);

    @CmpField(column = "LastName")
    @LocalMethod()
    public abstract String getLastName();

    @LocalMethod()
    public abstract void setLastName(String arg);

    @CmpField(primkeyField = Constants.Bool.TRUE, column = "Customer_ID")
    @LocalMethod()
    public abstract Integer getCustomer_ID();

    @LocalMethod()
    public abstract void setCustomer_ID(Integer arg);

    public java.lang.Integer ejbCreate(java.lang.String FirstName,
            java.lang.String LastName) {
        setFirstName(FirstName);
        setLastName(LastName);

        return null;
    }

    public void ejbPostCreate(java.lang.String FirstName,
            java.lang.String LastName) {
    }
}

Related Topics

@AutomaticKeyGeneration Annotation

@JarSettings Annotation

 

Skip navigation bar   Back to Top