Adding JPA support
The Java Persistence API (JPA) is one of the frameworks that equips Java with object/
relational mapping. Within JPA, a query language is provided that supports the developers
abstracting the underlying database.
With the release of JPA 2.0, there are many areas that were improved, such as:
- Domain Modeling
- EntityManager
- Query interfaces
- JPA query language and others
We are not going to study the inner workings of JPA in this recipe. If you wish to know more
about JPA, visit http://jcp.org/en/jsr/detail?id=317 or
http://download.oracle.com/javaee/5/tutorial/doc/bnbqa.html.
NetBeans provides very good support for enabling your application to quickly create entities
annotated with JPA.
In this recipe, we will see how to configure your application to use JPA. We will continue
to expand the previously-created project.
Getting ready
We will use GlassFish Server in this recipe since it is the only server that supports Java EE 6
at the moment.
We also need to have Java DB configured. GlassFish already includes a copy of Java DB in its
installation folder. Another source of installed Java DB is the JDK installation directory. If you
wish to learn how to configure Java DB, please refer to Chapter 4, JDBC and NetBeans.
It is not necessary to build on top of the previous recipe, but it is imperative to have a
database schema. Feel free to create your own entities by following the steps presented
in this recipe.
How to do it…
- Right-click on EJBApplication node and select New Entity Classes from Database….
- In Database Tables: Under Data Source, select jdbc/sample and let the IDE initialize
Java DB. - When Available Tables is populated, select MANUFACTURER, click Add, and then
click Next.

- In Entity Classes: leave all the fields with their default values and only in Package,
enter entities and click Finish.
How it works…
NetBeans then imports and creates our Java class from the database schema, in our case the
Manufacturer.java file placed under the entities package.
Besides that, NetBeans makes it easy to import and start using the entity straightaway. Many
of the most common queries, for example find by name, find by zip, and find all, are already
built into the class itself.
The JPA queries, which are akin to normal SQL queries, are defined in the entity class itself.
Listed below are some of the queries defined in the entity class Manufacturer.java:
@Entity @Table(name = "MANUFACTURER") @NamedQueries({ @NamedQuery(name = "Manufacturer.findAll", query = "SELECT m FROM Manufacturer m"), @NamedQuery(name = "Manufacturer.findByManufacturerId", query = "SELECT m FROM Manufacturer m WHERE m.manufacturerId = :manufacturerId"),
The @Entity annotation defines that this class, Manufacturer.java, is an entity and
when followed by the @Table annotation , which has a name parameter, points out the
table in the Database where the information is stored.
The @NamedQueries annotation is the place where all the NetBeans-generated JPA queries
are stored. There can be as many @NamedQueries as the developer feels necessary. One of
the NamedQueries we are using in our example is named Manufacturer.findAll, which
is a simple select query. When invoked, the query is translated to:
SELECT m FROM Manufacturer m
On top of that, NetBeans implements the equals, hashCode, and toString methods. Very useful
if the entities need to be used straight away with some collections, such as HashMap.
Below is the NetBeans-generated code for both hashCode and the toString methods:
@Override public int hashCode() { int hash = 0; hash += (manufacturerId != null ? manufacturerId.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Manufacturer)) { return false; } Manufacturer other = (Manufacturer) object; if ((this.manufacturerId == null && other.manufacturerId != null) || (this.manufacturerId != null && !this.manufacturerId. equals(other.manufacturerId))) { return false; } return true; }
NetBeans also creates a persistence.xml and provides a Visual Editor, simplifying the
management of different Persistence Units (in case our project needs to use more than one);
thereby making it possible to manage the persistence.xml without even touching the
XML code. A persistence unit , or persistence.xml , is the configuration file in JPA which
is placed under the configuration files, when the NetBeans view is in Projects mode. This file
defines the data source and what name the persistence unit has in our example:
<persistence-unit name="EJBApplicationPU" transaction-type="JTA"> <jta-data-source>jdbc/sample</jta-data-source> <properties/> </persistence-unit>
The persistence.xml is placed in the configuration folder, when using the Projects view. In
our example, our persistence unit name is EJBApplicationPU , using the jdbc/sample as
the data source.
To add more PUs, click on the Add button that is placed on the uppermost right corner of the
Persistence Visual Editor.
This is an example of adding another PU to our project:







Pingback: JavaPins