Java Reference
In-Depth Information
concepts and vice versa. Every plain old Java object (POJO) or JavaBean that needs to be made persis-
tent can be directly mapped by the ORM to one or more tuples in a relational database without hav-
ing to implement (vendor-) specific interfaces or classes. The ORM provides full support of all CRUD
( CREATE , READ , UPDATE , and DELETE ) database operations. A key advantage of using an object rela-
tional mapper is that it waives the Java developer from fully understanding and mastering all details of
relational database design and advanced SQL query tuning, since all database interactions are directly
handled and optimized by the ORM. This will result in more compact Java code, a potential decrease
in database calls, more efficient queries, and higher portability across database platforms.
A second solution is to abandon the idea of using a relational database and use an object-oriented
DBMS (OODBMS) instead. OODBMSs support the storage of objects rather than relational tuples.
This will allow the storage of all POJOs directly and transparently as objects in an OODBMS with-
out having to perform any translation. Obviously, avoiding all the mapping operations will greatly
benefit the performance of the Java application.
The Java community has introduced various standards for object persistence. The most popular are the
Java Persistence API (JPA) and Java Data Objects (JDO). Both provide a set of rules and guidelines to
make POJOs persistent for both Java SE and Java EE environments using either an ORM, OODBMS, or
another storage format (such as XML). JPA was mainly designed for relational databases, whereas JDO
was more targeted toward OODBMSs, which are less popular in the industry. Hence, given the wide-
spread availability of relational databases, this discussion will continue with JPA. For more details about
JDO, refer to http://www.oracle.com/technetwork/java/index-jsp-135919.html .
JPA was first developed in 2006. The most recent version, JPA 2.1, was released in 2013. The JPA
API is specified in the javax.persistence package. To facilitate the mapping between the Java
application and the database (either relational or OO), annotations are used. JPA also includes the
Java Persistence Query Language (JPQL) as an SQL-like standard to define object-oriented queries
on objects. JPQL queries are also formulated as Select... From... queries but can now return
objects rather than field values, as is the case in SQL. When using an ORM, the JPQL queries will
be translated to relational SQL queries, whereas when using an OODBMS, the JPQL queries can be
directly executed on the database, thereby increasing the performance of the query. JPQL provides
full support of all OO concepts such as (multiple) inheritance, dynamic binding, polymorphism, and
so on. Example implementations of the JPA standard are:
ORM: Hibernate, EclipseLink (formerly Oracle TopLink), and OpenJPA
OODBMS: ObjectDB
It is important to keep in mind that JPA was developed after many of the ORMs (such as Hibernate
and TopLink) were already available. The result is one overall, uniform interface that facilitates
switching between different persistence providers.
Two examples to illustrate the use of JPA follow. Hibernate is an example of an ORM implementa-
tion of JPA, and ObjectDB is an example of an OODBMS implementation of JPA.
hibernate
We will start by discussing Hibernate, one of the most commonly used ORMs in the industry. It is
an open-source ORM currently maintained by Red Hat at http://hibernate.org/ . It provides
 
Search WWH ::




Custom Search