Table of Contents
We show how to develop, deploy and run a simple example app using the TomEE web server, which is Apache Tomcat extended by adding basic Java EE features for providing an execution environment for light-weight Java EE web apps. We assume that you have already installed the TomEE Plume server and MySQL. Then simply follow our installation and configuration instructions.
The purpose of our minimal example app is to manage information about
books. For simplicity, we deal with a single object type
as depicted in Figure 4.1.
The following table shows a sample data population for the model class
Table 4.1. Sample data about books
|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 us e 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 in general all we need to do. In our example application, we explain how to set up the JPA configuration for MySQL.
Java is strongly typed: Properties, parameters and variables must be declared to be of some type.
No global variables, no global procedures: In Java, variables, procedures and functions must be defined in the context of a class, which provides their name space.
Arrays are static: Arrays have a fixed size, which cannot be changed at run-time.
No object without a class: For creating an object, a class has to be used (or defined) for
defining the properties of the object's property slots
defining the methods and functions that can be applied to the object (and all other objects instantiating the class)
Classes, properties and methods are defined with a visibility level (
private), which restricts their
Type parameters: Classes and complex data structures (such as lists) can be defined with the help of type parameters. See, for instance, the generics tutorial by Oracle.
Java programs must be compiled before they can be executed.
In Java, visibility levels are used to define the possible levels of access:
Table 4.2. Java Visibility Level
Normally, properties are defined as private, such that they can only be assigned with a public setter, which allows to control, and possibly log, any change of the property value. Unfortunately, Java does not allow to protect only write, but not read access. Therefore, we always have to define getters along with setters, even if they are not needed.
A Java bean
class (or, simply, bean
class) is a Java class with a parameter-free
constructor where all properties are serializable and have a
set method (also called "getter" and
"setter"). A Java bean is an object created with the help of a bean
A JPA entity
class (or, simply, entity
class) is a Java bean class with an
@Entity annotation, implying that a Java EE runtime
environment (such as provided by the TomEE PLUS
web server) will take care of the persistent storage of its
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 based on SQL. JPQL query expressions look very 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 view technology. By contrast, JSF 1 has used JavaServer Pages (JSP) as its default view technology.