Over a million developers have joined DZone.

Java Persistence API Introduction (Part 2)

DZone's Guide to

Java Persistence API Introduction (Part 2)

As we continue to look at the Java Persistence API, we look at CRUD methods, querying data stores, and the plugin ecosystem at your disposal.

· Java Zone ·
Free Resource

Learn how to stop testing everything every sprint and only test the code you’ve changed. Brought to you by Parasoft.

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.

  • @PersistenceContext, EntityManager, 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. That being said, they have gotten 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 b.id = ?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 = query.select(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 ecosystem. 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.

Get the top tips for Java developers and best practices to overcome common challenges. Brought to you by Parasoft.

java ee ,java ,persistence api ,queries ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}