Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
Every EJB 2.1 entity bean with bean-managed persistence must have a primary key field.
You can configure the primary key as a well-known Java type (see "Configuring a Primary Key Field for an EJB 2.1 Entity Bean With Bean-Managed Persistence") or as a special type that you create (see "Configuring a Primary Key Class for an EJB 2.1 Entity Bean With Bean-Managed Persistence").
For more information, see "What is a Primary Key of an Entity Bean With Bean-Managed Persistence?"
For EJB 2.1 entity beans with bean-managed persistence, you are responsible for assigning primary key values, typically in the ejbCreate
method (see "Configuring a Life Cycle Callback Method for an EJB 2.1 Entity Bean With Bean-Managed Persistence").
For a simple EJB 2.1 entity bean with bean-managed persistence, you can define your primary key to be a well-known Java type as follows:
Code your bean's ejbCreate
method to return the primary key class type (see "Implementing an EJB 2.1 Entity Bean With Bean-Managed Persistence")
Configure your deployment XML to use it (see "Using Deployment XML")
Example 15-1 shows the ejb-jar.xml
file <entity>
element <prim-key-class>
and <primkey-field>
subelements configured to specify a primary key as well-known Java type Integer
.
Example 15-1 ejb-jar.xml for Primary Key Field With Type Integer of EJB 2.1 Entity Bean With Bean-Managed Persistence
<enterprise-beans> <entity> <display-name>Employee</display-name> <ejb-name>EmployeeBean</ejb-name> <local-home>employee.EmployeeLocalHome</local-home> <local>employee.EmployeeLocal</local> <ejb-class>employee.EmployeeBean</ejb-class> <persistence-type>Bean</persistence-type> <prim-key-class>java.lang.Integer</prim-key-class> <reentrant>False</reentrant> <cmp-version>2.x</cmp-version> <abstract-schema-name>Employee</abstract-schema-name> <cmp-field><field-name>empNo</field-name></cmp-field> <cmp-field><field-name>empName</field-name></cmp-field> <cmp-field><field-name>salary</field-name></cmp-field> <primkey-field>empNo</primkey-field> </entity> ... </enterprise-beans>
If your primary key is more complex than a well-known Java data type, then you can define your own primary key class.
Your primary key class must have the following characteristics:
be named <
name
>PK
be public
and serializable
provide a constructor for creating a primary key instance
Your class may contain any number of instance variables used to form the primary key. Instance variables must have the following characteristics:
primitive object types
serializable types
types that can be mapped to SQL types
types that are a legal Value Type in RMI-IIOP
types that provide a suitable implementation of the hashCode()
and equals(Object)
methods
Once the primary key class is defined (see "Using Java"), to use it in an EJB, you must do the following:
Code your bean's ejbCreate
method to return the primary key class type (see "Implementing an EJB 2.1 Entity Bean With Bean-Managed Persistence")
Configure your deployment XML to use it (see "Using Deployment XML")
Example 15-2 shows an example primary key class.
Example 15-2 Primary Key Class Implementation for a EJB 2.1 Entity Bean With Bean-Managed Persistence
package employee; import java.io.*; import java.io.Serializable; ... public class EmployeePK implements java.io.Serializable { public Integer empNo; public EmployeePK() { this.empNo = null; } public EmployeePK(Integer empNo) { this.empNo = empNo; } }
As Example 15-3 shows, you define the primary key class within the ejb-jar.xml
file <prim-key-class>
element. You define each primary key class instance variable in a <cmp-field><field-name>
element using the same variable name as that used in the primary key class.
Example 15-3 ejb-jar.xml for Primary Key Class and Its Instance Variables of EJB 2.1 Entity Bean With Bean-Managed Persistence
<enterprise-beans> <entity> <description>no description</description> <display-name>EmployeeBean</display-name> <ejb-name>EmployeeBean</ejb-name> <local-home>employee.LocalEmployeeHome</home> <local>employee.LocalEmployee</remote> <ejb-class>employee.EmployeeBean</ejb-class> <persistence-type>Bean</persistence-type> <prim-key-class>employee.EmployeePK</prim-key-class> <reentrant>False</reentrant> <cmp-version>2.x</cmp-version> <abstract-schema-name>Employee</abstract-schema-name> <cmp-field><field-name>empNo</field-name></cmp-field> <cmp-field><field-name>empName</field-name></cmp-field> <cmp-field><field-name>salary</field-name></cmp-field> </entity> </enterprise-beans>