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, making queries type-safe and intuitive to read and write. The library is designed to enhance the API of the underlying JPA provider (e.g., Hibernate) without replacing existing software components. This allows for seamless integration of JPAStreamer into the current codebase.
Here is an example of a typical Stream query, highlighting the similarities between 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 |
JPAStreamer does not materialize the Stream. Instead, it inspects the Stream pipeline and merges the operations into a standard HQL query that is executed in the database. |
Installation
Prerequisites
To use JPAStreamer, make sure 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 metamodel. A common example is a standard Hibernate application with JPA Entities that map to database objects.
Maven Installation
To use JPAStreamer in your Quarkus application, add the JPAStreamer Quarkus extension to your build file. With Maven, add the following dependency to your POM file:
<dependency>
<groupId>io.quarkiverse.jpastreamer</groupId>
<artifactId>quarkus-jpastreamer</artifactId>
<version>3.0.3.Final</version>
</dependency>
You can also add the JPAStreamer dependency automatically with the following command:
quarkus extension add 'io.quarkiverse.jpastreamer:quarkus-jpastreamer'
Instantiating JPAStreamer
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.
Here is an example of how JPAStreamer can be instantiated with CDI Injection in a Hibernate application:
@ApplicationScoped public class FilmRepository { @Inject JPAStreamer jpaStreamer; (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 | Inject an instance of JPAStreamer |
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 |
JPAStreamer and Panache
When running JPAStreamer alongside Panache in a Quarkus Hibernate application, there are a few things to consider. 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 injected in a Panache Repository as follows:
@ApplicationScoped
public class FilmRepository implements PanacheRepository<Film> {
@Inject
JPAStreamer jpaStreamer; (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 | Inject an instance of JPAStreamer |
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 |
Demo Repository
If you want to see JPAStreamer in a more complete context, check out the JPAStreamer demo repository here.
Licence
The JPAStreamer Quarkus extension is released under the LGPL 2.1 License.