I have a question about primary keys in BMP beans. In some sample codes, I
have seen that the primary key is kept in a PK class and there is no field
in the bean class itself for the primary key. But in some other codes, there
IS a field corresponding to the primary key in the bean class in addition to
the PK class.
public class PersonPK implements java.io.Serializable {
public String personID;
.....
}
and
public class PersonBean implements EntityBean {
protected EntityContext ctx;
//
// Bean-managed state fields
//
private String id; // PK
private String name;
.............
}
Now my question is: How can the container associate these two primary keys?
How can it now that the field id in the bean class is PK? This association
is not mentioned in deployment descriptor, because we have just the
<prim-key-class>PersonPK</prim-key-class> there.
The first thing that comes to mind is that in EJBLoad() we use
ctx.getPrimaryKey() to get the primary key stored in PersonPK and store it
in the field "id". But consider this case:
Suppose that our key is 1 and hence both "ctx.getPrimaryKey().PersonID" and
the field "id" in the bean class evaluate to 1. Then we call setID(20) on
the bean class.
This will change the value of the field "id" to 20, but what will be the
value of "ctx.getPrimaryKey().PersonID" ? Will it change too? If yes, how?
How the container knows that "id" is a special field and its changes should
be reflected to the PersonPK object stored in the entity context?