Java Reference
In-Depth Information
@Entity(access=AccessType.FIELD)
class PendingOrder {
@Id(generate = GeneratorType.AUTO)
private int id;
private int state = PendingOrder.NEW;
@ManyToOne
private Restaurant restaurant;
@OneToMany(cascade = CascadeType.ALL)
private List<PendingOrderLineItem> lineItems
= new ArrayList<PendingOrderLineItem>();
@ManyToOne
private Coupon coupon;
…
The
@Entity
annotation specifies that the
PendingOrder
class is an entity bean,
and the
access=AccessType.FIELD
member tells the
EJB
container to map its fields
rather than its properties to the database. The
@Id
annotation identifies the pri-
mary key field and tells the
EJB
container to generate a primary key. The
@OneTo-
Many
annotation specifies that the
lineItems
field is a one-to-many relationship,
and the
@ManyToOne
annotation specifies that the
restaurant
and
coupon
fields are
many-to-one relationships. The
EJB 3
persistence mechanism uses the information
specified by the annotations in the same way that the
JDO
or Hibernate imple-
mentation uses the
XML
O/R
mapping documents.
This example uses the default
EJB 3
O/R
mapping rules that generate default
table and column names and define the mappings for relationships. The
PendingOrder
class is mapped to the
PENDINGORDER
table, the
id
field is
mapped to the
ID
column, and the
lineItems
field is mapped to a join table
called
PENDING_ORDER_PENDING_ORDER_LINE_ITEM
, which has foreign keys
to the
PENDINGORDER
and
PENDINGORDERLINEITEM
tables. You can, however,
use annotations to specify the names of the tables and columns and change how
some relationships are mapped. Later in this chapter you'll see examples of how
to do that.
EJB 3
encourages developers to use annotations to define an
EJB
, but you can
still use
XML
deployment descriptors. Whether you use annotations or deploy-
ment descriptors is largely a matter of personal preference, but there are situa-
tions in which deployment descriptors are useful. For example, the annotations
that define the
O/R
mapping can be verbose, and it can be easier to use a deploy-
ment descriptor instead. Another potential use for a deployment descriptor is to
Search WWH ::
Custom Search