An EJB 2.x Entity EJB bean class must implement the javax.ejb.EntityBean interface, which defines callback methods setEntityContext, unsetEntityContext, ejbActivate, ejbPassivate, ejbLoad, ejbStore, and ejbRemove that are called by the EJB container. An EJB 2.x provides implementation for the callback methods in the interface. An EJB 2.x entity bean also includes the ejbCreate and ejbPostCreate callback methods corresponding to one create method in the home interface. An EJB 2.x entity bean's component and home interfaces extend the EJBObject/EJBLocalObject and EJBHome/EJBLocalHome interfaces respectively. In comparison, an EJB 3.0 entity bean class is a POJO which does not implement the EntityBean interface. The callback methods are not implemented in the EJB 3.0 entity bean class. Also, the component and home interfaces and deployment descriptors are not required in EJB 3.0. The EJB configuration information is included in the Entity bean POJO class using metadata annotations. An EJB 2.1 entity bean also consists of getter/setter CMP (Container Managed Persistence) field methods, and getter/setter CMR (Container Managed Relationships) field methods. An EJB 2.x entity bean also defines finder and ejbSelect methods in the home/local home interfaces for EJB-QL queries. An example EJB 2.x entity bean is listed next:
In EJB 2.x, the ejb-jar.xml deployment descriptor defines the EJB-QL for finder methods. An example finder method is specified in the ejb-jar.xml as follows:
An EJB 3.0 entity bean is a POJO class annotated with the @Entity annotation. The finder methods are specified in the entity bean class itself using the @NamedQuery annotation. The EJB 3.0 entity bean persistence annotations are defined in the javax.persistence package. Some of the EJB 3.0 persistence annotations are presented in the following table:
The EJB 3.0 entity bean class corresponding to the EJB 2.x entity bean class is annotated with the metadata annotation @Entity. The finder method findByJournal in the EJB 2.x bean class is specified in the EJB 3.0 POJO class with the @NamedQuery annotation. The @Id annotation specifies the identifier property catalogId. The @Column annotation specifies the database column corresponding to the identifier property catalogId. If a @Column annotation is not specified for a persistent entity bean property, the column name is the same as the entity bean property name. Transient entity bean properties are specified with the @Transient annotation. The EJB 3.0 entity bean POJO class corresponding to the EJB 2.x entity bean is listed next:
An EJB 2.x entity bean instance is created with the create() method in the entity bean home/local home interface. A client for an EJB 2.x entity bean obtains a reference for the entity bean with JNDI lookup; CatalogLocalHome is the JNDI name of the CatalogBean entity bean:
To access the getter/setter methods of an entity bean, the remote/local object in EJB 2.x is obtained with the finder methods:
An entity bean instance is removed with the remove() method:
In EJB 3.0, persistence and lookup are provided by the EntityManger class. In a session bean client class for the EJB 3.0 entity bean, dependency injection is used to inject an EntityManager object using the @PersistenceContext annotation:
An entity bean instance is created by invoking new on the CatalogBean class and persisted with the persist() method of the EntityManager class:
An entity bean instance is obtained with the find() method:
A Query object for a finder method is obtained with the createNamedQuery method:
An entity bean instance is removed with the remove() method of the EntityManager class:
The client class for the EJB 3.0 entity bean is listed next: