Understanding Enterprise JavaBeans 3.0 2-3
2.2 WebLogic Server Value-Added EJB 3.0 Features
The following features are not part of the Enterprise JavaBeans 3.0 specification, but rather, are value-added features to further simplify the EJB 3.0 programming model:
■
Automatic persistence unit deployment based on variable name. If you want to query and update an entity in your session bean, you annotate a
variable with either the javax.persistence.PersistenceContext or javax.persistence.PersistenceUnit annotation; the variable is then
injected with persistence unit information. You can specify the unitName attribute to reference a particular persistence unit in the persistence.xml file of the EJB
JAR file; in this case, the EJB container automatically deploys the persistence unit and sets its JNDI name to the persistence unit name, as listed in the
persistence.xml file. You can also simply name the injected variable exactly the same as the persistence unit you want injected into the variable; in this case,
you no longer need to specify the unitName attribute, but the EJB container still deploys the persistence unit and sets its JNDI name to the persistence unit name.
See Section 5.3.4, Invoking a 3.0 Entity
for general information about invoking an entity from a session bean and
Section A.4, Annotations Used to Interact With Entity Beans
for reference information about the annotations.
■
Support for vendor-specific subinterfaces when injecting an EntityManager from a particular persistence provider.
When you inject a persistence context into a variable using either javax.persisetence.PersistenceContext or
javax.persistence.PersistenceUnit, the standard data type of the injected variable is EntityManager. If your persistence provider provides a
subinterface of the EntityManager such as KodoEntityManager in the case of Oracle Kodo then as a convenience you can simply set the data type of the
injected variable to that subinterface, rather than use the more complicated lookup mechanisms as described in the EJB 3.0 specification. For example:
PersistenceContext private KodoEntityManager em; See
Section 5.3.4, Invoking a 3.0 Entity and KodoEntityManager for general
information about EntityManager and the Oracle Kodo-provided KodoEntityManager.
2.3 EJB 3.0 Examples
See Chapter 3, Simple Enterprise JavaBeans 3.0 Examples,
for simple examples of stateless and stateful session beans, interceptor classes, and how to invoke an entity.
The sections in this guide reference these examples extensively. These examples are meant to simply show how to use the new EJB 3.0 metadata annotations and
programming model, rather than how to program the business code of your EJB.
For a more complex example that includes actual business code, see samples directory of the WebLogic Server installation, in particular WL_
HOMEsamplesserverexamplessrcexamplesejbejb30, where WL_HOME refers to the installation directory of WebLogic Server, such as
OracleMiddlewarewlserver_10.3.
2-4 Programming Enterprise JavaBeans, Version 3.0, for Oracle WebLogic Server
2.4 Programming 3.0 Entities
This guide describes how to program 3.0 session and message-driven EJBs, and how to invoke 3.0 entities from a session EJB. It does not describe how to actually program
and configure a 3.0 entity. For details instructions on that topic, see Java Persistence API in the Oracle Kodo documentation.
3
Simple Enterprise JavaBeans 3.0 Examples 3-1
3
Simple Enterprise JavaBeans 3.0 Examples
The following sections describe simple Java examples of EJBs that use the new metadata annotation programming model:
■
Section 3.1, Example of a Simple Stateless EJB
■
Section 3.2, Example of a Simple Stateful EJB
■
Section 3.3, Example of an Interceptor Class
■
Section 3.4, Example of Invoking a 3.0 Entity From A Session Bean Later procedural sections of this guide that describe how to program an EJB make
reference to these examples.
3.1 Example of a Simple Stateless EJB
The following code shows a simple business interface for the ServiceBean stateless session EJB:
package examples; Business interface of the Service stateless session EJB
public interface Service { public void sayHelloFromServiceBean;
}
The code shows that the Service business interface has one method, sayHelloFromServiceBean, that takes no parameters and returns void.
The following code shows the bean file that implements the preceding Service interface; the code in bold is described after the example:
package examples; import static javax.ejb.TransactionAttributeType.;
import javax.ejb.Stateless; import javax.ejb.TransactionAttribute;
import javax.interceptor.ExcludeDefaultInterceptors; Bean file that implements the Service business interface.
Class uses following EJB 3.0 annotations: - Stateless - specifies that the EJB is of type stateless session
- TransactionAttribute - specifies that the EJB never runs in a transaction
- ExcludeDefaultInterceptors - specifies any configured default interceptors should not be invoked for this class
3-2 Programming Enterprise JavaBeans, Version 3.0, for Oracle WebLogic Server
Stateless TransactionAttributeNEVER
ExcludeDefaultInterceptors public class ServiceBean
implements Service {
public void sayHelloFromServiceBean { System.out.printlnHello From Service Bean;
} }
The main points to note about the preceding code are:
■
Use standard import statements to import the metadata annotations you use in the bean file:
import static javax.ejb.TransactionAttributeType.; import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute; import javax.interceptor.ExcludeDefaultInterceptors
The annotations that apply only to EJB 3.0 are in the javax.ejb package. Annotations that can be used by other Java Platform, Enterprise Edition Java EE
Version 5 components are in more generic packages, such javax.interceptor or javax.annotation.
■
The ServiceBean bean file is a plain Java file that implements the Service business interface; it is not required to implement any EJB-specific interface. This
means that the bean file does not need to implement the lifecycle methods, such as ejbCreate and ejbPassivate, that were required in the 2.X programming
model.
■
The class-level Stateless metadata annotation specifies that the EJB is of type stateless session.
■
The class-level TransactionAttributeNEVER annotation specifies that the EJB never runs inside of a transaction.
■
The class-level ExcludeDefaultInterceptors annotation specifies that default interceptors, if any are defined in the ejb-jar.xml deployment
descriptor file, should never be invoked for any method invocation of this particular EJB.
3.2 Example of a Simple Stateful EJB