Quarkus JPASteamer Documentation

This is an excerpt of the full JPAStreamer docs. For the full documentation, see JPAStreamer User Guide.

JPAStreamer is an extension for JPA applications developed by Speedment Inc. It allows JPA queries to be expressed as standard Java Streams, which means queries are type-safe and intuitive to read and write. The library is designed to augment the API of the underlying JPA provider, .e.g Hibernate, without the need for replacing existing software components, thus JPAStreamer can be integrated without effecting the current codebase.

Here is an example of a typical Stream query, highlighting the similarities between the Stream and CRUD operations.

    jpaStreamer.stream(Film.class) (1)
                .filter(Film$.title.startsWith("A")) (2)
                .sorted(Film$.length.reversed()) (3)
                .limit(limit); (4)
1 FROM
2 SELECT
3 ORDER BY
4 LIMIT
It is important to notice that JPAStreamer does not materialize the Stream, but inspects the Stream pipeline and merge the operations to a standard HQL query which is executed in the database.

Installation

Prerequisites

JPASteamer requires that the following criteria are fulfilled:

  • JDK 11+ installed and correctly mapped to JAVA_HOME

  • A Quarkus database application that relies on a JPA provider and has a JPA meta model. A common example is a standard Hibernate application with JPA Entities that maps to database objects.

Maven Installation

To use JPAStreamer in your Quarkus application, simply add the JPAStreamer Quarkus extension to your build file. With Maven, this is done by adding the following dependency to your POM file:

<dependency>
    <groupId>io.quarkiverse.jpastreamer</groupId>
    <artifactId>quarkus-jpastreamer</artifactId>
    <version>1.0.1</version>
</dependency>

You can also add the JPAStreamer dependency automatically with the command:

quarkus extension add 'io.quarkiverse.jpastreamer:quarkus-jpastreamer'

JPAStreamer and Panache

Using the JPAStreamer Quarkus extension is no different from using the standard JPAStreamer library. Thus the JPAStreamer User Guide is the most complete reference during development.

There are however a few things to consider if running JPAStreamer alongside Panache in a Quarkus Hibernate application. The most recent version of JPAStreamer does not have support for Panache’s Active Record Pattern. This means the resulting JPAStreamer metamodel will most likely be erroneous. Therefore, we currently recommend Panache’s Repository Pattern.

JPAStreamer can be initialized in a Panache Repository as follows:

@ApplicationScoped
public class FilmRepository implements PanacheRepository<Film> {

    private final JPAStreamer jpaStreamer = JPAStreamer.of(this::getEntityManager); (1)

    public String startsWitASortedByLength(short limit) {
        return jpaStreamer.stream(Film.class) (2)
                .filter(Film$.title.startsWith("A")) (3)
                .sorted(Film$.length.reversed()) (4)
                .limit(limit); (5)
    }
}
1 Instantiate JPAStreamer with a Supplier<EntityManager>
2 Selects the Film entity as the stream source
3 Only selects films with a title that starts with "A"
4 Sort the films by descending length (longest first)
5 Limit the results to a given number of films

Native Builds

JPAStreamer is compatible with native builds, thus runs on Graal VM if the Quarkus application is built for native use.

Licence

The JPAStreamer Quarkus extension is released under the LGPL 2.1 License.

Short Video

Here is a short 1-minute video describing JPAStreamer:

Contributing

We gladly welcome any form of contributions, whether it is comments and questions, filed issues or pull requests.

Before we can accept your patches we need to establish a common legal ground to protect your rights to your contributions and the users of JPAStreamer. This is done by signing a Contributor License Agreement (CLA) with Speedment, Inc. The details of this process is laid out here.