Tutorial Home
Hibernate
StrutsJSP
Spring
|
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:
public class ItemKey implements java.io.Serializable {
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:
|
|
SCJP 6.0 Simulator 664+ Questions With Explanations
SCWCD 5.0 Simulator 556+ Questions With Explanations
SCWCD 4.0 Simulator 500+ Questions With Explanations
OCA 10g Simulator 594+ Questions With Explanations
Hiring made easy Harness social networking, employee referrals, Agency portal and Job Portals