The J2EETM Tutorial

Primary Keys for Container-Managed Persistence

If the primary key class does not belong to the J2SE or J2EE standard libraries, then 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), then you need to provide a customized primary key class.

The Primary Key Class

In the following example, the PurchaseOrderKey class implements a composite key for the PurchaseOrderEJB 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 String getProductModel() {	
        return productModel;	
    public String getVendorId() {	
        return vendorId;	
    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();	

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

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 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 phone call to a support center, the primary key might include a time stamp that indicates when the call was received. 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, and you can locate the bean by invoking its findByPrimaryKey method.