Java Persistence API Introduction (Part 2)
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.
Join the DZone community and get the full member experience.Join For Free
Get the Edge with a Professional Java IDE. 30-day free trial.
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); cq.select(cq.from(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.
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) .from(book) .where(book.id.eq(1)) .fetchOne();
Hibernate OGM provides JPA support for NoSQL data stores.
How about learning a little about the following Java EE technologies:
- Context and Dependency Injection (CDI)
- Enterprise Java Beans (EJB)
- JSP, JSP, and Expression Language
- RESTful web services
JAX-RS for RESTful Webservices
I have recently posted a mini-series of blogs taking a look at JAX-RS. They discuss:
- How to manage bean validation failure,
- Work with consumers and producers, and
- How to create JAX-RS resource entities.
There are two deep dive series on JAX-RS topics:
- What are JAX-RS annotations? A deep dive looking at the annotations most commonly used when developing REST endpoints.
- What is the @Context annotation used for? A deep dive into the many uses of the @Context annotation.
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.
Published at DZone with permission of Alex Theedom , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.