JPAstreamer Documentation

An Open Source Library to Instantly Process Data as Java Streams

JPAstreamer is a library for expressing JPA/Hibernate/Spring queries using standard Java Streams. With a single dependency, your application can immediately operate on database elements using standard Stream operators e.g. filter(), sort() and map(). As a result, your queries are more expressive, intuitive and less error-prone.

For example, starting with a standard JPA Entity you can effortlessly derive intuitive stream queries:

@Entity
@Table(name = "film", schema = "sakila")
public class Film implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "film_id", nullable = false, updatable = false, columnDefinition = "smallint(5)")
    private Integer filmId;

    @Column(name = "title", nullable = false, columnDefinition = "varchar(255)")
    private String title;

    @Column(name = "length", columnDefinition = "smallint(5)")
    private Integer length;

    @Column(name = "rating", columnDefinition = "enum('G','PG','PG-13','R','NC-17')")
    private String rating;
}

To operate on the elements of the table, JPAStreamer is first initialized with a simple builder (in this case using a persistence unit named "sakila"):

JPAStreamer jpaStreamer = JPAStreamer.createJPAStreamerBuilder("sakila")
    .build();

The obtained streamer is then used to create Streams that are rendered to database queries through JPA. For example:

jpaStreamer.stream(Film.class)
    .filter(Film$.rating.equal("G"))
    .sorted(Film$.length.reversed().thenComparing(Film$.title.comparator())) (1)
    .skip(10)
    .limit(5)
    .forEach(System.out::println);

This will print films rated G in reversed length order (where films of equal length will be in title order) but skipping the first ten and then printing only the following five films.

Film$ is automatically generated from the Film-table entity at compile-time by JPAStreamer as part of its metamodel.
More examples are available in the chapter Stream Examples.

How JPAstreamer fits into your application

JPAstreamer is a library that is added to enrich the API of the underlying JPA provider. It does not impact the existing application code and there is no need to replace any software components. Below is an overview of a typical database application leveraging JPAstreamer.

JPAstreamer Architecture

Application Code

The API is extended to include Java Streams as a way of writing type-safe queries. This means the application code typically consists of a combination of regular JPA queries and Stream pipelines. As a result, developers are free to choose a more expressive, intuitive and less error-prone way of fetching data.

JPAstreamer

JPAstreamer is added as a lightweight dependency to the Maven/Gradle build. It integrates with the JPA layer by rendering the Stream queries to JPA and passing them to the underlying JPA provider for execution.

JPA Layer

JPAstreamer can integrate with any existing JPA provider. The JPA layer remains responsible for the persisting and reading of objects in the database.

JDBC (SQL) Layer and Relational Database

Since the JPA provider is responsible for the database connection (most often) via JDBC, any database compatible with the chosen JPA provider can be used with JPAstreamer.

Where to begin

If this is your first time getting to know JPAstreamer, you may want to know Why JPAstreamer is a great addition to your database application and How JPAstreamer Works. After becoming familiar with the main concepts, you can follow the Quick-start Guide to get set up in minutes.

In case you don’t have an existing project at hand to use for testing, there is a pre-configured demo that can be located in the JPAstreamer Demo Repository.

If you wish to learn more about the Java Stream API, start with the chapter Stream Fundamentals.

Phone Home

JPAstreamer sends certain data back to Speedment’s servers as described here. If you wish to disable this feature, please contact us at info@jpastreamer.org.