About ABIS
What is available?ABIS Infor » 2017-09 » 2017-01 » 2016-11 » 2016-06 » 2016-04 » 2016-02 » 2015-03 » 2014-03 » 2013-04 » 2013-01 » 2012-10 » 2012-06 » 2012-04 » 2012-01 » 2011-11 » Object-Oriented Perl » Oracle and Objects » Hibernate and JPA2 » ABIS and vICTory-be: no-nonsense! » 2011-10 » 2011-08 » 2011-06Exploring Db2Presentations and papersTest your knowledge 

ABIS Infor - 2011-11

Hibernate and JPA2

Gie Indesteege (ABIS) - 30 September 2011


Persisting Java objects to a relational database can be done in several ways: using the native JDBC API or working with a persistence framework, that avoids a lot of plumbing work and more over offers additional features like transactional support, security, caching, ... The chase for a good persistence framework is open.

Java and persistency

The life time of objects in an application is limited. Keeping objects for future use is a challenge. Persisting objects in a database causes a real headache because of the object/relational mismatch. So let's look for some possible options.

Once you have decided which object attributes are to be persisted into a database, you can write your own Java code to create, read, update or delete (CRUD) the information in/from the RDBMS of your choice. The JDBC API provides all you need to interact with the database through classes like Connection, Statement and ResultSet and their associated methods. A small example to retrieve person information for all persons with a name starting with 'P':

   String querySQL = "Select pno, plname, pfname from persons" 
                     + " where plname like '" + "P%'"
   Connection con = DriverManager.getConnection(dbUrl, user, password);
   Statement stmt = con.createStatement();
   ResultSet rs = stmt.executeQuery(querySQL);
   while (rs.next()) { System.out.println(rs.getString("pno") + ", " +
                      rs.getString("pfname") + ", " + rs.getString("plname")); }

It works fine, but you have to do it all yourself: know SQL, setup and control the connection to the database, handle the type conversion and the resultset, catch exceptions (not shown in the code), decide on the transactional level ... A little relief of effort is possible by using 'preconfigured' DataSources, which will be in charge for controlling the connection to the database.

But the alternative of using a persistence framework, reduces the programming effort more drastically and moreover offers transactional features, security options, performance improvements, ... for free. So what is the offer?

Persistence frameworks


Java Persistence API is part of the JSR 220 (EJB 3) specification (part of Java EE 6), created as a common denominator of the proposals made by the Java Community Process for handling persistence in enterprise Java applications.

Persistence in this context covers three areas:

How can I access the data from an object oriented point of view? What options are available to synchronise my objects with the database?

I want to 'tune' the generated SQL or write my own complex query.

How can I associate the object model with the database structure?


JPA is built on the strength of Java SE annotations, completed with an extensive implementation library and a flexible configuration. So if you want to save a person entity to the database:

2. Don't forget that you do need a persistence provider - in the example we use EclipseLink:

Configuration persistence.xml

   <persistence xmlns="http://java.sun.com/xml/ns/persistence"...>
   <persistence-unit name="abis" transaction-type="RESOURCE_LOCAL">
         <property name="javax.persistence.jdbc.driver"
         <property name="javax.persistence.jdbc.url" value="jdbc:derby:c:\DerbyDB"/>


2. Next define the entity to be mapped to the database:

Entity class Person.java

   public class Person{
       public Person() {}
       @Id @GeneratedValue
       private int personNumber;
       private String firstName, lastName;
       // getters and setters of attributes


2. Finally use the mapped entity object and bring it under supervision of the (configured) entity manager, controlled by a transactional context:

Sample program

   EntityManagerFactory emf = Persistence.createEntityManagerFactory("abis");
   EntityManager em = factory.createEntityManager();
   EntityTransaction tx = em.getTransaction();
   Person employee = new Person();
   //set some properties here

And that's all you need to do for persisting a person object into the database. Amazing? The real power however can be discovered in the more advanced features of cascaded updating, caching, lazy loading, list processing, querying, ... to name a few.



as developed by JBoss (Gavin King), is an implementation of the JPA spec (and yes, Hibernate is perhaps the most important influencer of the JPA spec), with additional options.


So you can see Hibernate as a superset of JPA. Classes and method names are different, but the idea is the same: provide a transparent persistency to the application developer.










+ (specific) Hibernate annotations

JPQL (JPA Query Language)

HQL (Hibernate Query Language)
QBC (Query By Criteria)
QBE (Query By Example)

Another differentiator is that Hibernate offers additional features like logging, security, historical data, choice of connection pooling mechanisms, ... which adds to the value.

Other persistence frameworks like OpenJPA (Apache), MyBatis (Google), EclipseLink (Eclipse) or DataNucleus (Apache) are all based on the same ideas. An extensive list can be found on http://java-source.net/open-source/persistence.

Hibernate and JPA2

The standard JPA specification is evolving, perhaps under the 'pressure' of Hibernate and other influencers. The actual JPA2 (JSR 317) adds a.o.

These additional features are, as a matter of fact, also available in Hibernate.


Extending the life of your objects by persisting them to the database is no longer a 'plumber' activity based on JDBC. When you set up/configure a persistence framework and annotate the domain classes correctly, the correct synchronisation between your objects and the data base becomes a fact, even in a multi-user environment. However, keep your eyes open for tuning the configuration to optimal performance, whether you use pure JPA or enhanced with Hibernate (or other) features.


A more detailed and practical insight can be obtained by attending the ABIS course on "Hibernate and JPA" (http://www.abis.be/html/en1309.html)