|
Home TOC Index |
|
Search
Feedback |
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
PurchaseOrderKeyclass implements a composite key for thePurchaseOrderEJBentity bean. The key is composed of two fields,productModelandvendorId, whose names must match two of the persistent fields in the entity bean class.public class PurchaseOrderKey implements java.io.Serializable { 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) && vendorId.equals( ((PurchaseOrderKey)other).vendorId)); } return false; } public int hashCode() { return productModel.concat(vendorId).hashCode(); } }For container-managed persistence, a primary key class must meet the following requirements:
- The access control modifier of the class is
public.- All fields are declared as
public.- The fields are a subset of the bean's persistent fields.
- The class has a public default constructor.
- The class implements the
hashCode()andequals(Object other)methods.- The class is serializable.
Primary Keys in the Entity Bean Class
In the
PurchaseOrderBeanclass, the following access methods define the persistent fields (vendorIdandproductModel) 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
ejbCreatemethod of thePurchaseOrderBeanclass. The return type of theejbCreatemethod is the primary key, but the return value isnull. Although not required, thenullreturn 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 { setVendorId(vendorId); setProductModel(productModel); setProductName(productName); 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 the deployment descriptor, the primary key class is defined as a
java.lang.Object. The primary key field is not specified.- In the home interface, the argument of the
findByPrimaryKeymethod must be ajava.lang.Object.- In the entity bean class, the return type of the
ejbCreatemethod must be ajava.lang.Object.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
getPrimaryKeymethod, and you can locate the bean by invoking itsfindByPrimaryKeymethod.
|
Home TOC Index |
|
Search
Feedback |