Table of Contents
JPA is a Java API for the management of persistent data in Java applications. It uses the Java Persistence Query Language (JPQL), a platform-independent object-oriented query language, which is heavily inspired by SQL, and its query expressions are quite similar to SQL query expressions, but they are executed in the context of JPA entity objects.
JSF is a Java specification for building component-based user interfaces for web applications. Its current version, JSF 2, by default, uses Facelets as its template technology. In contrast, JSF 1 has used JavaServer Pages (JSP) as its default template technology.
In this tutorial, we show how to develop, deploy and run a simple example app using the TomEE web server, which provides an execution environment for Java/JPA/JSF web apps. We assume that you have already installed the TomEE PLUS web server, the MySQL DBMS, and the ANT build tool on your computer.
The purpose of our example app is to manage information about books. For simplicity, in this
chapter we deal with a single object type
Book, as depicted in Figure 3.1.
The following is a sample data population for the
Table 3.1. Sample data for
|006251587X||Weaving the Web||2000|
|0465026567||Gödel, Escher, Bach||1999|
|0465030793||I Am A Strange Loop||2008|
What do we need for a data management app? There are four standard use cases, which have to be supported by the app:
Create a new book record by allowing the user to enter the data of a book that is to be added to the collection of stored book records.
Retrieve (or read) all books from the data store and show them in the form of a list.
Update the data of a book record.
Delete a book record.
These four standard use cases, and the corresponding data management operations, are often summarized with the acronym CRUD.
For entering data with the help of the keyboard and the screen of our computer, we use HTML forms, which provide the user interface technology for web applications.
For any data management app, we need a technology that allows to store data in persistent records on a secondary storage device, such as a hard-disk or a solid state disk. JPA allows using a great number of different data storage technologies, including many SQL database management systems (DBMS) such as Oracle, MySQL and PostgreSQL. We don't have to change much in the application code for switching from one storage technology to another. Adding the right driver implementation to our Java runtime environment, properly setting up the DBMS and changing the database access configuration is all we need to do. Below, in step 3, we explain how to set up the JPA configuration for MySQL.
In the first step, we set up our folder structure for the application program code. The application name in this chapter will be "Public Library", and we will use a corresponding name for the application folder, "publicLibrary". Then we create the application structure. There are many ways to do this, like for example to use the Eclipse development environment (and create and configure a Dynamic Web Project). In this tutorial we show how to do it manually, so there is no need to use special tools, except ANT for being able to compile and deploy the application more easily. For your convenience, we provide an ANT script (available for download at the end of this tutorial), which is able to create the folder structure of a web application, compile it to a Web application Archive (WAR) file and then deploy it to a TomEE web server for execution. The application structure (which is compatible with the Dynamic Web Project structure of Eclipse, so it can be imported in Eclipse) is the following:
publicLibrary src pl model ctrl META-INF persistence.xml WebContent views books WEB-INF templates faces-config.xml web.xml
This folder structure has the following parts:
src folder contains the app code folder
pl, defining the
Java package name for the app (as a shortcut of 'public library'), and the folder
META-INF with configuration files:
the app code folder
pl contains the model and controller code in its
ctrl, while the view/UI code is contained in
the most important configuration file (and the only one we need for this app) is the
persistence.xml file. It contains the configuration for the database
connection. The content of this file is discussed in Section 3.2.
WebContent folder contains various web resources, including template
files and custom view files:
views stores our custom view files for the application, so it
represents the View part of the MVC paradigm. Please note that it is not strictly required to
name it views, but it makes a lot of sense to do it so, since this is what this folder
represents. We will discuss in details about its content, later in this tutorial.
WEB-INF folder contains the used libraries (
jar files) of
the project (as part of the
lib subfolder), the JSF template files for your
pages (as part of the
templates subfolder), the
file, which stores the facelets configuration data and the
file, specific to the Tomcat (TomEE) environment server used to run our application.