Identify correct and incorrect statements or examples about EJB 3.0 / EJB 2.x interoperability, including how to adapt an EJB 3.0 bean for use with clients written to the EJB 2.x API and how to access beans written to the EJB 2.x API from beans written to the EJB 3.0 API.

[Note]

Interoperability of EJB 3.0 and Earlier Components

This release of Enterprise JavaBeans supports migration and interoperability among client and server components written to different versions of the EJB APIs.

Clients written to the EJB 2.x APIs

An enterprise bean that is written to the EJB 2.1 or earlier API release may be a client of components written to EJB 3.0 API using the earlier EJB APIs when deployed in an EJB 3.0 container.

Such an EJB 2.1 or earlier client component does not need to be rewritten or recompiled to be a client of a component written to the EJB 3.0 API.

Such clients may access components written to the EJB 3.0 APIs and components written to the earlier EJB APIs within the same transaction.

Clients written to the new EJB 3.0 API

A client written to the EJB 3.0 API may be a client of a component written to the EJB 2.1 or earlier API.

Such clients may access components written to the EJB 3.0 APIs and components written to the earlier EJB APIs within the same transaction.

Such clients access components written to the earlier EJB APIs using the EJB 2.1 client view home and component interfaces. The EJB annotation (or the ejb-ref and ejb-local-ref deployment descriptor elements) may be used to specify the injection of home interfaces into components that are clients of beans written to the earlier EJB client view.

Combined use of EJB 2.x and EJB 3.0 persistence APIs

EJB clients may access EJB 3.0 entities and/or the EntityManager together with EJB 2.x entity beans together within the same transaction as well as within separate transactions.

NOTE: In general, the same database data should not be accessed by both EJB 3.0 and EJB 2.x entities within the same application: behavior is unspecified if data aliasing occurs.

Adapting EJB 3.0 Session Beans to Earlier Client Views

Clients written to the EJB 2.1 and earlier client view depend upon the existence of a home and component interface.

A session bean written to the EJB 3.0 API may be adapted to such earlier preexisting client view interfaces.

The session bean designates the interfaces to be adapted by using the RemoteHome and/or LocalHome metadata annotations (or equivalent deployment descriptor elements).

When the client is deployed, the container classes that implement the EJB 2.1 home and remote interfaces (or local home and local interfaces) referenced by the client MUST provide the implementation of the javax.ejb.EJBHome and javax.ejb.EJBObject interfaces (or the javax.ejb.EJBLocalHome and javax.ejb.EJBLocalObject interfaces) respectively.

In addition, the container implementation classes must implement the methods of the home and component interfaces to apply to the EJB 3.0 component being referenced.

Adapting EJB 3.0 Stateless Session Beans

The invocation of the home create() method must return the corresponding local or remote component interface of the bean. This may or may not cause the creation of the bean instance, depending on the container's implementation strategy. For example, the container may preallocate bean instances (e.g., in a pooling strategy) or may defer the creation of the bean instance until the first invocation of a business method on the bean class. When the bean instance is created, the container invokes the setSessionContext method (if any), performs any other dependency injection, and invokes the PostConstruct lifecycle callback method(s) (if any).

It is likewise implementation-dependent as to whether the invocation of the EJBHome remove(Handle) or EJBObject or EJBLocalObject remove() method causes the immediate removal of the bean instance, depending on the container's implementation strategy. When the bean instance is removed, the PreDestroy callback method (if any) is invoked.

The invocations of the business methods of the component interface are delegated to the bean class.

Adapting EJB 3.0 Stateful Session Beans

The invocation of a create<METHOD>() method causes construction of the bean instance, invocation of the PostConstruct callback (if any), and invocation of the matching Init method, and returns the corresponding local or remote component interface of the bean. The invocation of these methods occurs in the same transaction and security context as the client's call to the create method.

The invocation of the EJBHome remove(Handle) or EJBObject or EJBLocalObject remove() method causes the invocation of the the PreDestroy callback method (if any) and removal of bean instance.

The invocations of the business methods of the component interface are delegated to the bean class.

The Init annotation is used to specify the correspondence of a method on the bean class with a create method of the adapted EJBHome and/or EJBLocalHome interface. The result type of such an Init method is required to be void, and its parameter types must be exactly the same as those of the referenced create<METHOD>() method.

Combined Use of EJB 3.0 and EJB 2.1 APIs in a Bean Class

This document describes the typical usage of annotations to specify the enterprise bean type and callback methods. It is permitted to combine the use of such annotations with the bean's implementation of one of the javax.ejb.EnterpriseBean interfaces as such combination may be useful in facilitating migration to the EJB 3.0 simplified programming model.

In addition to the business interface, a session bean may define EJBHome, EJBLocalHome, EJBObject, and/or EJBLocalObject interfaces in accordance with the rules of the EJB 2.1 specification. A deployment descriptor or metadata annotations may be used to associate the bean class with these interfaces.

EJB 2.1 Client / EJB 3.0 Component

Enterprise bean components, which need to work with EJB 2.1 and earlier specifications, don't need to be written using old specifications any more. EJB 3.0 beans can utilize metadata annotations to make them work with older clients that expect to access them using the home and the remote interface. The methods required as per older specifications are mapped to corresponding methods in the enterprise bean written using the EJB 3.0 specification. As an example, the create() method as desired in the old specification could now map to a method that initializes the bean.

Older beans written to EJB 2.1 client view can talk to new components

Example:

// EJB 2.1 client view of 3.0 bean
...
Context initialContext = new InitialContext();
ShoppingCartHome myCartHome = (ShoppingCartHome)
	initialContext.lookup("java:comp/env/ejb/cart");
ShoppingCart cart= myCartHome.create();
cart.addItem(...);
...
cart.remove();
...
					

Using EntityManager API from EJB 2.x Stateless Session Bean

The javax.persistence.EntityManager API is used for creating, finding, updating entity bean instances. The MySessionEJB EJB 2.1 stateless session bean uses EntityManager API to create Employee EJB 3.0 bean instances. The stateless EJB makes a JNDI lookup to obtain an instance of EntityManager and uses persist method on EntityManager instance to create EJB 3.0 entity bean objects:

public class MySessionEJBBean implements SessionBean {

	public void addEmployee(int empNo, String name, double sal) {

		Employee emp = null;

		try {
			Context ctx = new InitialContext();
			EntityManager em =
				(EntityManager) ctx.lookup("java:comp/env/persistence/EntMngr");

			emp = new Employee();
			emp.setEmpNo(empNo);
			emp.setName(name);
			emp.setSal(sal);
			em.persist(emp);

		} catch (Exception ex) {
			...
		}
	}

	public void ejbCreate() {
	}
	...
}
					

Please note this release makes EntityManager available in ENC by using the persistence-context-ref. For packaging EJB 2.x beans with EJB 3.0 beans or entities you need to specify the version="3.0" in the ejb-jar tag in the deployment descriptor as follows:


<ejb-jar version="3.0">
	<enterprise-beans> 
		<session>
			<ejb-name>MySessionEJB</ejb-name>
			.. 
			<persistence-context-ref>
				<persistence-context-ref-name>
					persistence/EntMngr
				</persistence-context-ref-name>
				<persistence-unit-name>
					HRDept
				</persistence-unit-name>
			</persistence-context-ref>
		</session>
	</enterprise-beans>
</ejb-jar>
					
					

EJB 3.0 Client / EJB 2.1 Component

EJB 3.0 beans can access EJB 2.1 and earlier beans. Dependency injection is utilized to inject the EJB 2.1 component references. JNDI lookup calls are avoided. Once a handle is gained to the injected EJB home interface, the create() method is called to instantiate and subsequently utilize the bean.

Beans written to new APIs can be clients of older beans

Example:

// EJB 3.0 client view of 2.1 bean
...
@EJB ShoppingCartHome cartHome;
Cart cart = cartHome.create();
cart.addItem(...);
...
cart.remove();
...
					

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine