Java Persistence API (JPA) is a collection of classes and methods to persistently store the vast amounts of data into a database which is provided by the Oracle Corporation. JPA is used for mapping Java objects to the tables of relational databases that is stable and experienced in many companies. It has Java Persistence Query Language (JPQL) that support different database dialects and allows the application has not changed code when changing database.

- JPA is a specification for which several implementations exist. NetBeans supports several JPA implementations including Toplink, Hibernate, KODO, and OpenJPA.
=> Hibernate can implement JPA api OR uses its own language (for example, HQL, Criteria)
Can Hibernate  implements JDO ? Currently talking to JDO is as an ORM that is independent with hibernate. Hibernate used for RDBMS while JDO is for object DBMS.
  • JDO and JPA are both specifications, not implementations
  • The idea is you can swap JPA implementations, if you restrict your code to use standard JPA only
  • Hibernate can be used as one such implementation of JPA.
  • However, Hibernate provides a native API, with features above and beyond that of JPA.
- Make sure you evaluate the DataNucleus implementation of JDO.


There are two standard API's for persistence - Java Data Objects (JDO) and Java Persistence API (JPA). The former (JDO) is designed for all datastores, and the latter is designed for RDBMS datastores only. When choosing the API to use in your application you should bear the following factors in mind.
- If you are familiar with Hibernate, you might think of the JPA mappedBy as the equivalent of the Hibernate inverse attribute, which indicates which end of a relationship should be ignored.

Progress of JPA development something like:
  • JDBC
  • Data Mappers (MyBatis, ..)
  • EJB
  • JDO
  • JPA (1.0 ~ EJB3.0, 2.0 went final in 2009, 2.1  in 2013, JPA 2.2 (and EJB 3.2)  in June 2017)

The JPA consists of four areas:

  • The Java Persistence API
  • The Java Persistence Criteria API
  • The Query language
  • Object-relational mapping metadata

JPA relational mapping

There are 4 types of relations:
  • OneToOne
  • ManyToOne
  • OneToMany
  • ManyToMany
uni/bidirectional Relations, (eagerly) or on-demand (lazily)

Bidirectional one-to-one relationships
Every relationship has two sides:

  • The owning side is responsible for propagating the update of the relationship to the database. Usually this is the side with the foreign key.
  • The inverse side maps to the owning side.

In the one-to-one mapping in the example application, the Invoice object is the owning side. Listing 4 demonstrates what the inverse side -- the Order -- looks like.

JPA - Queries

There are 3 kinds of queries :
  • Native queries (native SQL)
  • Java Persistence Query Language (object oriented version of SQL): NamedQuery /NamedQueries, TypedQuery, Query
  • Using the Criteria API
Named Queries are static, @NamedQuery  is compiled & prevent from SQL injections.  Queries (JPQL, native)  can also be dynamic that are created at runtime by the application.
Criteria API is more dynamic and strongly typed.

JPA Inheritance

There are 3 strategies to map inheritance:
  • Single Table (default) - SINGLE_TABLE
  • Table per concrete class - TABLE_PER_CLASS
  • Joined Table - JOINE
SINGLE_TABLE: This strategy maps all classes to one single table so that a query will never has to use a join, leading to be increase performance. The disadvantage is the redundant stored data.
JOINED: In this strategy, corresponding to each type will create a separate table but it not duplicate shared data accepting primary key. We have to do joining queries in using which can decrease query speed significantly.
TABLE_PER_CLASS: the same as JOINED strategy BUT this strategy creates a separate table for each entity type. There is no need to use join queries. The share of inheritance hierarchy data will be duplicate stored in each table.

In Google App EngineJOINED&SINGLE_TABLE strategies are not supported. The "TABLE_PER_CLASS" and "MAPPED_SUPERCLASS" strategies work as described in the DataNucleus documentation.
In JOINED strategy of normal Java EE invironment, it should add @Inheritance(strategy = InheritanceType.JOINED) into super entity class.

Hibernatge Dialect Property & Collection: