Key classes and key fields
A key class is used when creating or finding an entity bean. It represents the identity of the entity bean, corresponding to the primary-key columns of a row in a relational database. Each designated container-managed field in the entity bean that corresponds to one of these columns is called a key field.
There are two main ways to specify primary key classes:
- Single-field keys are primary keys that map to a single field in the entity bean class. If you only have one key field whose type is a valid IIOP type, then this type may be used as the prim-key-class entry in the deployment descriptor. When doing so, the field must be specified as a prim-key-field. If you have a container-managed entity with a prim-key-class and you try to add another attribute to the key of this CMP bean, you will be prompted to change the key class since you can no longer have a prim-key-field.
- Compound keys are primary keys that map to multiple fields in the entity bean class. You must declare the primary key class as well as all fields in the primary key class as public.
For 1.1 CMP beans, for each key field in the bean class, there must be a field with the same name and type in the key class; conversely, for every field in the key class, there must be a field with the same name and type in the bean class, and these fields are considered to be key fields. For 2.x CMP beans, there must be a get and set method for each key field.
For example, suppose that you have an EJB 1.1 Account bean for a banking application. The accountNumber property has been designated as a key field, and it corresponds to the primary key of an ACCOUNT database table. The key class for Account (AccountKey) includes the following:
- A public String field called accountNumber
- A one-argument constructor, AccountKey(String), that sets accountNumber
As an alternative to AccountKey, Account could use a key class of java.lang.String, and a primary key field of accountNumber.
Key classes are common to all inherited enterprise beans. This means that the key class of a parent enterprise bean is also used by all of its child enterprise beans. So, if the key class is changed for an entity with subtypes or the inheritance structure changes, all subtype entities will have their corresponding Home and Bean classes regenerated. Key classes can be shared only among beans in the same inheritance hierarchy.
If you have roles as part of the key, and if the key shape of the role's enterprise bean type changes (added or removed keys), a dialog to propagate these changes through all the key roles is displayed. In most cases, you should select Yes or Yes to All. Not propagating the changes through all key roles could result in compilation errors.
Unknown primary key is not supported
The EJB tooling currently does not support the Unknown primary key definition described in the EJB 2.0 specification. The workaround is to define a specific primary key class.
Related tasks
Adding and removing CMP fields
Adding client views or interfaces to beans