Java Reference
In-Depth Information
All fields are declared as public.
The class has a public default constructor.
The accessors are public.
The class implements the
hashCode()
and
equals(Object other)
methods.
Listing 21-1
shows a primary class for the
PartEJB
that represents the business object stored in the
Part
table in the database. The
Part
table has a composite key: the
productId
and
vendorId
fields.
The primary key class then has two attributes:
productId
and
vendorId
.
Listing 21-1:
A primary key class example
public class PartKey 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 PartKey) {
return (productId.equals(((ItemKey)other).productId)
&& vendorId.equals(((ItemKey)other).vendorId));
}
return false;
}
public int hashCode() {
return productId.concat(vendorId).hashCode();
}
}
With BMP, the
ejbCreate
method (to be discussed in
next section
) assigns the input parameters to
instance variables and returns the primary key object. A client can fetch the primary key of an entity
bean by invoking the
getPrimaryKey
method of the
EJBObject
class as follows:
PartKey id = (PartKey)Part.getPrimaryKey();
The entity bean retrieves its own primary key by calling the
getPrimaryKey
method of the
EntityContext
class as shown here:
PartKey id = (PartKey)context.getPrimaryKey();