Skip Headers

Oracle® Application Server Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 2 (10.1.2)
Part No. B15505-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

How to map a CMP field and a CMR field to the same row in the database�

When you define a relationship between beans, you define a CMP field and then you map a relationship to the bean. Both the CMP field and the relationship eventually map to the same row in the database. For instance, in the employee bean, a department ID is specified as a CMP field. When you define the CMR relationship between the employee and the department bean, the employee/department relationship is mapped through a CMR defined variable (dept) that is mapped to the employee bean's department ID. Since both fields map to the same row in the database, you must specify that one of these fields cannot be changed through the shared attribute. The shared attribute makes the particular CMP or CMR field read-only. Thus, to define that the CMP field, deptID, cannot be modified, do the following:�

<cmp-field-mapping name="deptID" shared=true" persistence-name="deptID" />�

Note:

You cannot mark a Container-Managed Persistence (CMP) entity bean as read-only if it has a Container-Managed Relationships (CMR) relationship to another entity bean that is not read-only.

Defining the above in the employee entity bean states that the deptID field is "shared" and that you cannot modify the deptID to be another value through the setDeptID method. Instead, if you needed to modify the relationship, you would have to modify the relationship through the CMR relationship variable, dept, with the setDept method, pointing the employee entity bean to another bean (and thus, another row in the database).�

In the case of complex primary keys, if you have a CMR field as part of the relationship, you can define it as shared, and then it takes on the same attribute as a primary key in that no one can modify it after it is initially set.