Chapter 5. Combining Persistence with CDI

In earlier chapters we covered lots of Java EE ground, combining several technologies such as the CDI API. The examples so far, however, are based on a false assumption – that all the information can be stored in memory. In this chapter, we will show how to use a persistent data store for our application in the form of a standard relational database.

The Enterprise JavaBeans (EJB) 3.0 specification includes a persistence specification called the Java Persistence API (JPA). It is an API for creating, removing, and querying Java objects called entities that can be used within both a compliant EJB 3.0 Container and a standard Java SE environment.

There is a lot of ground to cover in this chapter and concepts will be coming at you from every direction. But at the end of it, you will be able to appreciate exactly how to create and deploy a complete Java EE 6 application.

Specifically, we will discuss the following topics:

  • The key elements of JPA
  • How to create your entities, starting with database tables
  • How to manipulate the entities using CDI Beans and EJBs
  • Delivering a frontend tier for our application using JSF and Facelets technology

Data persistence meets a standard

The arrival of an Enterprise Java Persistence standard based on the Plain Old Java Object (POJO) development model fills a substantial gap in the Java EE platform. The previous attempt (the EJB 2.x specification) missed the mark and created the stereotype of EJB entity beans being awkward to develop and too heavy for many applications. Therefore, it never achieved widespread adoption or general approval in many sectors of the industry.

Software developers knew what they wanted, but many could not find it in the existing standards, so they decided to look elsewhere. What they found was proprietary persistence frameworks, both in the commercial and open source domains.

In contrast to EJB 2.x Entity Beans, the EJB 3.0 Java Persistence API (JPA) is a metadata driven POJO technology. That is, to save data held in Java objects into a database, our objects are not required to implement an interface, extend a class, or fit into a framework pattern.

Another key feature of JPA is the query language, called the Java Persistence Query Language (JPQL), which gives you a way to specify the semantics of queries in a portable way, independent of the particular database you are using in an enterprise environment. JPA queries resemble SQL queries by syntax but operate against entity objects rather than directly with database tables.

Working with JPA

Inspired by ORM frameworks such as Hibernate, JPA uses annotations to map objects to a relational database. JPA entities are POJOs that do not extend any class nor implement any interface. You don't even need XML descriptors for your mapping. Actually, the JPA API is made up of annotations and only a few classes and interfaces. For example, we would mark the class Company as @Entity as follows:

@Entity
public class Company {
...
public Company () {  }

@Id
String companyName;
...

}

This little piece of code shows the minimal requirements for a class to be persistent. That is:

  • It must be identified as an entity using the @javax.persistence.Entity annotation
  • It must have an identifier attribute annotated with @javax.persistence.Id
  • It must have a no-argument constructor

Since you will learn better with an example, we will show how to create and deploy a sample JPA application on JBoss AS 7 in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.14.132