Home TOC |
Search
Feedback |
Primary Key Class
You specify the primary key class on the Entity tabbed pane of the deploytool. When deploying the ProductEJB bean, for example, you would specify a java.lang.String as the primary key class. In most cases, your primary key class will be a String or some other class that belongs to the java package.
Creating a Primary Key Class
For some entity beans, you will need to define your own primary key class. For example, if a primary key is composed of multiple fields then you must create a primary key class. In the following primary key class, the productId and vendorId fields together uniquely identify an entity bean:
public class ItemKey implements java.io.Serializable { public String productId; public String vendorId; public ItemKey() { }; public ItemKey(String productId, String vendorId) { this.productId = productId; this.vendorId = vendorId; } public String getProductId() { return productId; } public String getVendorId() { return vendorId; } public boolean equals(Object other) { if (other instanceof ItemKey) { return (productId.equals(((ItemKey)other).productId) && vendorId.equals(((ItemKey)other).vendorId)); } return false; } public int hashCode() { return productId.hashCode(); } }Class Requirements
A primary key class must meet these requirements:
- The access control modifier of the class is public.
- All fields are declared as public.
- For container-managed persistence, the field names in the primary key clsss must match the corresponding container-managed fields in the entity bean class.
- The class has a public default constructor.
- The class implements the hashCode() and equals(Object other) methods.
- The class is serializable.
Bean-Managed Persistence and the Primary Key Class
With bean-managed persistence, the ejbCreate method returns the primary key class:
public ItemKey ejbCreate(String productId, String vendorId, String description) throws CreateException { if (productId == null || vendorId == null) { throw new CreateException( "The productId and vendorId are required."); } this.productId = productId; this.vendorId = vendorId; this.description = description; return new ItemKey(productId, vendorId); }The ejbFindByPrimaryKey verifies the existence of the database row for the given primary key:
public ItemKey ejbFindByPrimaryKey(ItemKey primaryKey) throws FinderException { try { if (selectByPrimaryKey(primaryKey)) return primaryKey; . . . } private boolean selectByPrimaryKey(ItemKey primaryKey) throws SQLException { String selectStatement = "select productid " + "from item where productid = ? and vendorid = ?"; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, primaryKey.getProductId()); prepStmt.setString(2, primaryKey.getVendorId()); ResultSet rs = prepStmt.executeQuery(); boolean result = rs.next(); prepStmt.close(); return result; }Container-Managed Persistence and the Primary Key Class
Getting the Primary Key
A client can fetch the primary key of an entity bean by invoking the getPrimaryKey method of the EJBObject class:
Account account; . . . String id = (String)account.getPrimaryKey();The entity bean retrieves its own primary key by calling the getPrimaryKey method of the EntityContext class:
EntityContext context; . . . String id = (String) context.getPrimaryKey();
Home TOC |
Search
Feedback |