Java Reference
In-Depth Information
DTO reduces to just the essential data that the layer requests, thereby optimizing the transfer of
data across tiers. This chapter does not go into detail regarding the DTO. It's recommended that you
read the DTO chapter in the Core J2EE Patterns: Best Practices and Design Strategies book.
Java Persistence Architecture API and Object
Relational Mapping
The Java Persistence API (JPA) manages the application's interactions with the data source. It
specii es how to access, persist, and manage data between the application's objects and the data
source. JPA itself cannot perform CRUD or other data‐related operations; it's just a set of interfaces
and implementation requirements. However, a compliant Java EE application server must provide
support for its use.
The JPA specii cation replaces the EJB 2.0 Container‐Managed Persistence (CMP) entity beans spec-
ii cation, which was heavyweight and complex. CMP received an adverse reaction from many in the
developer community that resulted in the wide adoption of proprietary solutions such as Hibernate
and TopLink. This prompted the development of JPA (released with EJB 3.0), which aimed to bring
together CMP, Hibernate, and TopL i n k and seems to have been largely successful.
At the heart of JPA is the concept of an entity. For those of you familiar with CMP, this is what was
referred to as an entity bean . An entity is a short‐lived object that is capable of being persisted in a
database—not as a serialized object but as data. It is a Plain Old Java Object (POJO) whose mem-
bers are annotated and mapped to a i eld in the data source. To better understand how this is repre-
sented in code, you'll go through a code snippet.
In the following snippet, you represent a Movie entity class as a POJO that's appropriately annotated:
public class Movie {
@Id @GeneratedValue
private Long id;
private String title;
private String description;
private Float price;
public Movie(){}
// For brevity, the getters and setters have been left out.
As you can see, this is a simple class with just three annotations. The @Entity class level annotations
indicate that this class should be treated as an entity class, and the @Id and @Generated annotations
mark the id member as an auto‐generated identii cation i eld. This means that when the entity is per-
sisted, the id i eld is automatically generated according to the rules of auto‐generated i elds laid down
by the data source. If the data source is a database, then all i elds in this entity are persisted to a table
called Movie . No other annotations are necessary to indicate which i elds are persisted. It is convention
over coni guration that all i elds are persisted unless otherwise annotated. This mapping is referred to
as Object Relational Mapping (ORM). It is beyond the scope of this chapter to discuss in detail JPA
and ORM, so it is recommended that you read The Java EE 7 Tutorial: Part VIII Persistence . 2
Search WWH ::

Custom Search