EJB - Entity Beans III
4 April 2008This post is in continuation of EJB - Entity Beans II. Do read that one first. This post continues to address requirements about declaring primary keys for Entity beans.
A primary key class must meet the following requirements:
- class must be serializable
- access control modifier of the class must be public.
- all fields must be declared as public.
- fields must be a subset of the bean’s persistent fields.
- public default constructor is a must
- class should implement the hashCode() and equals(Object other) methods
Lets take an example. The example class has a composite primary key.
public class PurchaseOrderKey implements java.io.Serializable { public String productModel; public String vendorId; public PurchaseOrderKey() { }; 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(); } }
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: EJB - Entity Beans III