Java Reference
In-Depth Information
They are intended to be the standard Java object persistence mechanism and can
be used both inside and out of the application server.
EJB 3
persistence works out-
side the application server in the same way as
JDO
and Hibernate. Even if you are
only developing server applications, this is an extremely valuable feature because it
means that you can test entity beans without deploying them in the
EJB
container.
Simpler configuration
Another important improvement in
EJB 3
is that you are no longer required to
write complex
XML
deployment descriptors to describe a bean's configuration.
Instead,
EJB 3
lets you configure a bean using Java 5 annotations. An annotation is
a Java 5 language feature that associates extra data with a program element such
as a class or method. This data can be read by tools and frameworks such as the
EJB
container. Annotations are often easier to use than an
XML
deployment
descriptor because they are located next to the program element that they
describe. What's more, because
EJB 3
has sensible defaults for an
EJB'
s properties
you often only have to use a few annotations to turn a
POJO
into an
EJB
.
For example, the following code fragment shows the annotations on the
PlaceOrderFacade
interface and
PlaceOrderFacadeImpl
class that will deploy the
PlaceOrderFacade
from chapter 7 as a stateless session bean:
@Local
public interface PlaceOrderFacade {
…
}
@Stateless
public class PlaceOrderFacadeImpl
implements PlaceOrderFacade {
…
}
The
@Local
annotation specifies that the
PlaceOrderFacade
interface is a local
EJB
interface, and the
@Stateless
annotation specifies that
PlaceOrderFacadeImpl
is a
stateless session bean. By default, its
JNDI
name is the fully qualified class name of
the local
EJB
interface and the
EJB
container uses container-managed transactions
with a transaction attribute of
REQUIRED
. If necessary, you can override the defaults
by using additional annotations. The
EJB
container reads the information speci-
fied by the annotations and uses it to deploy the
EJB
.
Configuring an entity bean is equally straightforward. You annotate the
POJO
class with an
@Entity
annotation and annotate its fields or properties to map them
to the database. For example, here is part of the code for the
PendingOrder
EJB
:
Search WWH ::
Custom Search