News Ticker

Java Persistence API Introduction (Part 2)

Introduction to JPA

This is the second part of a two-part article introduces the Java Persistence API. In part one I talked about how to annotate a POJO with appropriate annotations in order to make the POJO into a persistable entity.

The Persistence Provider

The persistence provider can be configured to automatically create, update and remove a database schema based on the annotated entities. Entities are managed by the entity manager, which is represented by an EntityManager instance and defines the methods that are used to interact with the persistence context.

  • @PersistenceContextEntityManager, entityManager

These are essentially CRUD methods and are persist, find, merge and remove. Further functionality is provided via the entity manager instance that flushes changes to loaded entities to the database and allows for the execution of named queries.

How to Query the Data Store

The Java Persistence API provides various ways to query the data store.

JPQL provides a natural object oriented related way to query objects. It looks very familiar if you have used SQL and follows many of the same conventions. Although the subject of the query is the class name of the entity rather than the table name.

select b.isbn from Book b

It is very terse but does not always stand up well to refactoring in an IDE. Although they have got better in recent iterations.

Alternatively, the Criteria API provides a kind of API over JPQL that resembles a fluent APIs, although it is much more verbose, it does stand up to refactoring in an IDE.

EntityManager em = ...;
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Cargo> cq = em.getCriteriaBuilder().createQuery(Book.class);;
List<Book> AllBooks = em.createQuery(cq).getResultList();

If you like using SQL then native queries allow you to use the same queries you execute against the database directly in the Java code. The advantage is that you can use vendor specific syntax to get access to features that are not supported in JPQL, but you are coupling yourself to a particular database vendor.

If you have stored procedures they are supported too.

Plug-in Ecosystem

As is common there is also a small plug-in ecosystem.

The DeltaSpike Query module provides capabilities for implementing repository patterns which simplifying the repository layer. The way this is implemented should be very familiar to anyone who has used Spring Data and has used the @Query annotations on JPA repository interface methods to describe a query.

@Query("select b from Book b where = ?1")
Book findById(Long id);

Querydsl is a framework which enables the creation of type-safe SQL-like queries for different backends such as JPA, MongoDB, and SQL in Java.

Book book =

Hibernate OGM provides JPA support for NoSQL data stores.

Further Reading

How about learning a little about the following Java EE technologies:

JAX-RS for RESTful Webservices

I have recently posted a mini-series of blogs taking a look at JAX-RS. They discuss:

There are two deep dive series on JAX-RS topics:

What Next?

If you are new to Java EE it can be overwhelming to get your head around all the APIs the form the enterprise eco-system. That is why I wrote and recorded the video training course Learning Java Enterprise Edition. It is a two-hour course that introduces you to all the most important Java EE APIs. With plenty of demonstrations, code examples, and practice tasks on how to program with Enterprise Java, you will up to speak and well on your way to being a Java EE developer.

Advance Your Knowledge

If you want to learn more, there are courses that dive deeper into each of the APIs. There are courses:

Many more Java EE courses are planned on the horizon, so why not get in now and give your Java EE career a boost.

1 Trackback / Pingback

  1. Java Persistence API Introduction (Part 1)

Leave a Reply