Professional Documents
Culture Documents
Skip Headers
Skip Headers
Previous Next
Description
Home Interface
(remote or local)
Component
Interface (remote
or local)
Bean
implementation
1. Create the home interfaces for the bean (see "Implementing the EJB
2.1 Home Interfaces").
The remote home interface defines the create and finder methods that a
client can invoke remotely to instantiate your bean. The local home
interface defines the create and finder methods that a collocated bean
can invoke locally to instantiate your bean.
For more information about finders, see "Understanding Finder
Methods"
a. To create the remote home interface,
extend javax.ejb.EJBHome (see "Implementing the Remote Home
Interface").
b. To create the local home interface,
extend javax.ejb.EJBLocalHome (see "Implementing the Local Home
Interface").
2. Create the component interfaces for the bean (see "Implementing the
EJB 2.1 Component Interfaces").
The remote component interface declares the business methods that a
client can invoke remotely. The local interface declares the business
methods that a collocated bean can invoke locally.
a. To create the remote component interface,
extend javax.ejb.EJBObject (see "Implementing the Remote
Component Interface").
b. To create the local component interface,
extend javax.ejb.EJBLocalObject (see "Implementing the Local
Component Interface").
3. Define the primary key for the bean (see "Configuring a Primary Key for
an EJB 2.1 Entity Bean With Container-Managed Persistence").
The primary key identifies each entity bean instance and is a
serializable class. You can use a simple data type class, such
as java.lang.String, or define a complex class, such as one with two or
more objects as components of the primary key.
4. Implement the entity bean with container-managed persistence as
follows:
a. Implement the abstract getter and setter methods that
correspond to the getter and setter method(s) declared in the
home interfaces.
ebjCreate
5. Create the appropriate database schema (tables and columns) for the
entity bean.
For an entity bean with container-managed persistence, you can
specify how persistence attributes should be stored in the database or
you can configure the container to manage table creation for you.
For more information, see the following:
a. "Configuring Table and Column Information"
b. "Configuring Automatic Database Table Creation"
o Configure your ejb-jar.xml file to match your bean implementation
and to reference a data source defined in your data-sources.xml file
(see "Using Deployment XML").
o Complete the configuration of your entity bean (see "Using an
EJB 2.1 Entity Bean With Container-Managed Persistence").
Using Java
Example 13-1 shows a typical implementation of an EJB 2.1 entity bean with
container-managed persistence. Example 13-2 shows the corresponding
remote home interface and Example 13-3 shows the corresponding remote
component interface.
Example 13-1 Implementation of an EJB 2.1 Entity Bean With
Container-Managed Persistence
package cmpapp;
import javax.ejb.*;
import java.rmi.*;
public abstract class EmployeeBean implements EntityBean {
private EntityContext ctx;
// container-managed persistent fields accessors
public abstract Integer getEmpNo();
public abstract void setEmpNo(Integer empNo);
public abstract String getEmpName();
public abstract void setEmpName(String empName);
public abstract Float getSalary();
public abstract void setSalary(Float salary);
public void EmployeeBean() {
// Empty constructor, don't initialize here but in the create().
// passivate() may destroy these attributes in the case of pooling
}
public EmployeePK ejbCreate(Integer empNo, String empName, Float salary)
throws CreateException {
setEmpNo(empNo);
setEmpName(empName);
setSalary(salary);
return new EmployeePK(empNo);
}
public void ejbPostCreate(Integer empNo, String empName, Float salary)
throws CreateException {
// when just after bean created
}
public void ejbStore() {
// when bean persisted
}
public void ejbLoad() {
// when bean loaded
}
public void ejbRemove() {
// when bean removed
}
public void ejbActivate() {
// when bean activated
}
public void ejbPassivate() {
// when bean deactivated
}
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void unsetEntityContext() {
this.ctx = null;
}
}
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>Select OBJECT(e) From EmployeeBean e where e.empName = ?1</ejb-ql>
</query>
</entity>
</enterprise-beans>
...
Previous Next
Copyright 2002, 2009, Oracle and/or its affiliates. All rights reserved.
Legal Notices