CMP
Entity Bean With Container-Managed Persistence
1. Create the home interfaces for the bean
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 thecreate and finder methods that a collocated bean can invoke locally to instantiate your bean.
a. To create the remote home interface, extend javax.ejb.EJBHome
b. To create the local home interface, extend javax.ejb.EJBLocalHome
2. Create the component interfaces for the bean
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
b. To create the local component interface, extend javax.ejb.EJBLocalObject
3. Define the primary key for the bean
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.
For an entity bean with container-managed persistence, the getter and setter methods are public abstract, because the container is responsible for their implementation.
b. Implement the business methods that you declared in the home and component interfaces (if any). The signature for each of these methods must match the signature in the remote or local interface, except that the bean does not throw the RemoteException. Since both the local and the remote interfaces use the bean implementation, the bean implementation cannot throw the RemoteException.
For an entity bean, these methods are often delegated to a session bean
c. Implement any methods that are private to the bean or package used for facilitating the business logic. This includes private methods that your public methods use for completing the tasks requested of them.
d. Implement the ejbCreate methods that correspond to the create method(s) declared in the home interfaces. The container invokes the appropriateejbCreate method when the client invokes the corresponding create method.
The return type of all ebjCreate methods is the type of the bean's primary key.
For an entity bean with container-managed persistence, provide create methods that allow the client to pass in values that the container will persist to your database.
e. Provide an empty implementation for each of the javax.ejb.EntityBean interface container callback methods.
f. Implement a setEntityContext method (that takes an instance of EntityContext) and unsetEntityContext method
g. Optionally, define zero or more public, abstract select methods
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.
6. Configure your ejb-jar.xml file to match your bean implementation and to reference a data source defined in your data-sources.xml file
7. Complete the configuration of your entity
CMP Remote Component Interface
package cmpapp;
import javax.ejb.*;
import java.rmi.*;
public interface Employee extends EJBObject {
// container-managed persistent fields accessors
public Integer getEmpNo() throws RemoteException;
public void setEmpNo(Integer empNo) throws RemoteException;
public String getEmpName() throws RemoteException;
public void setEmpName(String empName) throws RemoteException;
public Float getSalary() throws RemoteException;
public void setSalary(Float salary) throws RemoteException;
}
CMP Remote Home Interface
package cmpapp;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
public interface EmployeeHome extends EJBHome {
public Employee create(Integer empNo, String empName, Float salary)
throws CreateException, RemoteException;
public Employee findByPrimaryKey(EmployeePK pk)
throws FinderException, RemoteException;
public Collection findByName(String empName)
throws FinderException, RemoteException;
public Collection findAll()
throws FinderException, RemoteException;
}
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 {
}
public void ejbStore() {
}
public void ejbLoad() {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void unsetEntityContext() {
this.ctx = null;
}
}
ejb-jar.xml
<enterprise-beans>
<entity>
<description>no description</description>
<display-name>EmployeeBean</display-name>
<ejb-name>EmployeeBean</ejb-name>
<home>cmpapp.EmployeeHome</home>
<remote>cmpapp.Employee</remote>
<ejb-class>cmpapp.EmployeeBean</ejb-class>
<persistence-type>Container</persistence-type>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>EmployeeBean</abstract-schema-name>
<prim-key-class>cmpapp.EmployeePK</prim-key-class>
<reentrant>False</reentrant>
<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>
<query>
<description></description>
<query-method>
<method-name>findAll</method-name>
<method-params/>
</query-method>
<ejb-ql>Select OBJECT(e) From EmployeeBean e</ejb-ql>
</query>
<query>
<description></description>
<query-method>
<method-name>findByName</method-name>
<method-params>
<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>
1. Create the home interfaces for the bean
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 thecreate and finder methods that a collocated bean can invoke locally to instantiate your bean.
a. To create the remote home interface, extend javax.ejb.EJBHome
b. To create the local home interface, extend javax.ejb.EJBLocalHome
2. Create the component interfaces for the bean
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
b. To create the local component interface, extend javax.ejb.EJBLocalObject
3. Define the primary key for the bean
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.
For an entity bean with container-managed persistence, the getter and setter methods are public abstract, because the container is responsible for their implementation.
b. Implement the business methods that you declared in the home and component interfaces (if any). The signature for each of these methods must match the signature in the remote or local interface, except that the bean does not throw the RemoteException. Since both the local and the remote interfaces use the bean implementation, the bean implementation cannot throw the RemoteException.
For an entity bean, these methods are often delegated to a session bean
c. Implement any methods that are private to the bean or package used for facilitating the business logic. This includes private methods that your public methods use for completing the tasks requested of them.
d. Implement the ejbCreate methods that correspond to the create method(s) declared in the home interfaces. The container invokes the appropriateejbCreate method when the client invokes the corresponding create method.
The return type of all ebjCreate methods is the type of the bean's primary key.
For an entity bean with container-managed persistence, provide create methods that allow the client to pass in values that the container will persist to your database.
e. Provide an empty implementation for each of the javax.ejb.EntityBean interface container callback methods.
f. Implement a setEntityContext method (that takes an instance of EntityContext) and unsetEntityContext method
g. Optionally, define zero or more public, abstract select methods
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.
6. Configure your ejb-jar.xml file to match your bean implementation and to reference a data source defined in your data-sources.xml file
7. Complete the configuration of your entity
CMP Remote Component Interface
package cmpapp;
import javax.ejb.*;
import java.rmi.*;
public interface Employee extends EJBObject {
// container-managed persistent fields accessors
public Integer getEmpNo() throws RemoteException;
public void setEmpNo(Integer empNo) throws RemoteException;
public String getEmpName() throws RemoteException;
public void setEmpName(String empName) throws RemoteException;
public Float getSalary() throws RemoteException;
public void setSalary(Float salary) throws RemoteException;
}
CMP Remote Home Interface
package cmpapp;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
public interface EmployeeHome extends EJBHome {
public Employee create(Integer empNo, String empName, Float salary)
throws CreateException, RemoteException;
public Employee findByPrimaryKey(EmployeePK pk)
throws FinderException, RemoteException;
public Collection findByName(String empName)
throws FinderException, RemoteException;
public Collection findAll()
throws FinderException, RemoteException;
}
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 {
}
public void ejbStore() {
}
public void ejbLoad() {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void unsetEntityContext() {
this.ctx = null;
}
}
ejb-jar.xml
<enterprise-beans>
<entity>
<description>no description</description>
<display-name>EmployeeBean</display-name>
<ejb-name>EmployeeBean</ejb-name>
<home>cmpapp.EmployeeHome</home>
<remote>cmpapp.Employee</remote>
<ejb-class>cmpapp.EmployeeBean</ejb-class>
<persistence-type>Container</persistence-type>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>EmployeeBean</abstract-schema-name>
<prim-key-class>cmpapp.EmployeePK</prim-key-class>
<reentrant>False</reentrant>
<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>
<query>
<description></description>
<query-method>
<method-name>findAll</method-name>
<method-params/>
</query-method>
<ejb-ql>Select OBJECT(e) From EmployeeBean e</ejb-ql>
</query>
<query>
<description></description>
<query-method>
<method-name>findByName</method-name>
<method-params>
<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>