Primary Keys for Container-Managed Persistence

Sometimes you must implement the class and package it along with the entity bean. For example, if your entity bean requires a composite primary key (which is made up of multiple fields) or if a primary key field is a Java programming language primitive type, then you must provide a customized primary key class.

The Primary Key Class

For container-managed persistence, a primary key class must meet the following requirements:

In the following example, the PurchaseOrderKey class implements a composite key for the PurchaseOrderBean entity bean. The key is composed of two fields--productModel and vendorId--whose names must match two of the persistent fields in the entity bean class.

public class PurchaseOrderKey implements {

  public String productModel;
  public String vendorId;

  public PurchaseOrderKey() { };

  public boolean equals(Object other) {

    if (other instanceof PurchaseOrderKey) {
      return (productModel.equals(
          ((PurchaseOrderKey)other).productModel) &&
    return false;

  public int hashCode() {

    return productModel.concat(vendorId).hashCode();

Primary Keys in the Entity Bean Class

In the PurchaseOrderBean class, the following access methods define the persistent fields (vendorId and productModel) that make up the primary key:

public abstract String getVendorId();
public abstract void setVendorId(String id);
public abstract String getProductModel();
public abstract void setProductModel(String name); 

The next code sample shows the ejbCreate method of the PurchaseOrderBean class. The return type of the ejbCreate method is the primary key, but the return value is null. Although it is not required, the null return value is recommended for container-managed persistence. This approach saves overhead because the bean does not have to instantiate the primary key class for the return value.

public PurchaseOrderKey ejbCreate (String vendorId, 
    String productModel, String productName) 
    throws CreateException {


    return null;

Generating Primary Key Values

For some entity beans, the value of a primary key has a meaning for the business entity. For example, in an entity bean that represents a player on a sports team, the primary key might be the player's driver's license number. But for other beans, the key's value is arbitrary, provided that it's unique. With container-managed persistence, these key values can be generated automatically by the EJB container. To take advantage of this feature, an entity bean must meet these requirements:

In these entity beans, the primary key values are in an internal field that only the EJB container can access. You cannot associate the primary key with a persistent field or any other instance variable. However, you can fetch the bean's primary key by invoking the getPrimaryKey method on the bean reference, and you can locate the bean by invoking its findByPrimaryKey method.

If you use deploytool to create the database tables, the SQL type of the table column will be set for you. If you create the tables, set the SQL type for the primary key column to NUMERIC (19) or BIGINT.