1 About JPA

In this Cloud Tutorial, we’ll use real data to replace the sample data from current version of the web application. This inclusion of real data will be made possible through use of the Java Persistence API (JPA), which lets us save entities in a database and retrieve them at a later date.

Version 2.1 of the Java Persistence API (JPA) was included in Java EE 7 for persisting entities in a database. The reference implementation of JPA 2.1 is EclipseLink 2.5[1]. WildFly 8.1.0, which we are using, uses a different well-known JPA implementation in the form of Hibernate 4.3.5, which offers JPA 2.1 support.

JPA enables us to store instances of entity classes in a relational database and retrieve them from the database at a later date. In this process, the entities are stored in a database table and one row is created for each instance. By convention, JPA uses the name of the entity to decide the database table in which it should be stored: specifically, it stores the entity in a table with the same name as the entity class. This means that if the name of the entity class is e.g. Campaign, instances of the entity will be stored in the CAMPAIGN table. If so desired, this convention can be broken with for a specific entity class; the developer must simply state the table name for this entity explicitly.

This process is known as object-relational mapping (see also chapter I.2.4 in the component architecture of JavaEE) and goes completely unnoticed by the developer, for whom the relational database – thanks to JPA – behaves just like an object-oriented database. The developer is spared the details in the background and accesses the persistence layer using an instance of the class EntityManager from the package javax.persistence. The EntityManager can be made available to the calling class very simply, using Dependency Injection.

For example: with EntityManager, the developer can persist an instance of the Campaign entity simply by invoking the method persist:

em.persist(campaign);

In the example, the identifiers for the EntityManager and the Campaign object are em and campaign. In reality, of course, it’s never that simple. The following chapters should help you resolve ambiguities.

Discussion

Use the message board below to give the authors your feedback or to discuss this page’s topic with other readers (in English please!). Please don’t expect the authors to answer directly, but they might update the content of this site according to your feedback.