Identify correct and incorrect statements or examples about an entity bean's primary key and object identity.

From the viewpoint of the Bean Provider, entity objects have a runtime object identity that is maintained by the Container.

The Container maintains the persistent identity of an entity object on the basis of its PRIMARY KEY.

The primary key of an entity bean may or may not be visible as one or more cmp-fields of the instance, depending on the way in which it is specified. Once it has been set, the Bean Provider MUST NOT attempt to change the value of a primary key field by means of a set method on its cmp-fields.

When a new instance of an entity bean whose primary key fields are visible in the entity bean class is created, the Bean Provider MUST use the ejbCreate<METHOD>(...) method to set all the primary key fields of the entity bean instance before the instance can participate in a relationship, e.g. be used in a set accessor method for a cmr-field. The Bean Provider MUST NOT reset a primary key value by means of a set method on any of its cmp-fields after it has been set in the ejbCreate<METHOD>(...) method.

The container must be able to manipulate the primary key type of an entity bean. Therefore, the primary key type for an entity bean with container-managed persistence MUST follow the rules:

There are two ways to specify a primary key class for an entity bean with container-managed persistence:

  1. Primary key that maps to a SINGLE field in the entity bean class.

    The Bean Provider uses the primkey-field element of the deployment descriptor to specify the container-managed field of the entity bean class that contains the primary key. The field’s type must be the primary key type.


  2. Primary key that maps to MULTIPLE fields in the entity bean class.

    The primary key class MUST be public, and MUST have a public constructor with NO PARAMETERS.

    ALL fields in the primary key class MUST be declared as public.

    The names of the fields in the primary key class MUST be a subset of the names of the container-managed fields.

public class ItemKey implements {
	public String productId; // public - mandatory
	public String vendorId; // public - mandatory
	public ItemKey() { }; // no-args constructor - mandatory
	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) { // mandatory
		if (other instanceof ItemKey) {
			return (productId.equals(((ItemKey)other).productId) 
			&& vendorId.equals(((ItemKey)other).vendorId));
		return false;

	public int hashCode() { // mandatory
		return productId.hashCode();
A primary key class MUST meet these requirements:

In special situations, the entity Bean Provider may choose not to specify the primary key class or the primary key fields for an entity bean with container-managed persistence. This case usually happens when the entity bean does not have a natural primary key, and/or the Bean Provider wants to allow the Deployer using the Container Provider’s tools to select the primary key fields at deployment time. The entity bean’s primary key type will usually be derived from the primary key type used by the underlying database system that stores the entity objects. The primary key used by the database system may not be known to the Bean Provider.

In this special case, entity bean must meet these requirements:

We are using 2 elements in DD for defining primary keys: <prim-key-class> and <primkey-field> :

The primary key class can be specific to a particular entity bean class. That is, each entity bean can define its own primary key class. Or multiple entity beans CAN share the same primary key class.

If two entity objects with the SAME home interface have the SAME primary key, they are considered IDENTICAL entity objects. If they have a different primary key, they are considered different entity objects.

There are 2 ways of comparing Entity Beans:

  1. Using isIdentical(EJBObject obj) method of javax.ejb.EJBObject interface on bean's REMOTE interface, or isIdentical(EJBLocalObject obj) method of javax.ejb.EJBLocalObject interface on bean's LOCAL interface.

    Item item1 = ...;
    Item item2 = ...;
    if (item1.isIdentical(item2)) {
    	// item1 and item2 refer to the same entity object

  2. Comparing primary keys of two entity object references (ONLY FROM THE SAME HOME INTERFACE !).

    ItemHome itemHome = ...; // get home interface
    Item item1 = itemHome.findByName(...);
    Item item2 = itemHome.findByCountry(...);
    if (item1.getPrimaryKey().equals(item2.getPrimaryKey())) {
    	// item1 and item2 refer to the same entity object

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine