JPAstreamer-logo

An Open Source Library to Express Hibernate/JPA Queries as Java Streams

1. Get a Stream from any JPA Entity

1

2

3

jpaStreamer.stream(User.class)

2. Apply a Stream pipeline

1

2

3

4

5

6

jpaStreamer.stream(User.class)

    .filter(User$.age.equal("22"))

    .sorted(User$.name.reversed())

    .skip(10)

    .limit(5)

    .forEach(System.out::println)

"JPAStreamer is really beautiful, and I will start using it to make my code more readable."

"Your framework seems to me to be one of the best and most beautiful code style projects I’ve ever seen for Java."

- Robert Zeranski

Head of Software Development, Dako

"Wicked cool!"

- StackOverflow user


Why JPAstreamer? 


JPAstreamer is a library for expressing JPA/Hibernate/Spring queries using standard Java streams. JPAstreamer instantly gives Java developers type-safe, expressive and intuitive means of obtaining data in database applications.

Run JPAStreamer natively with Quarkus


JPAStreamer is now available as a Quarkus extension, unlocking Java Stream queries for native builds.

Use type-safe Java Streams

JPAstreamer provides a fully type-safe query API. This helps to instantly detect errors and allows you to utilize code-completion for increased efficiency when coding. 


Avoid complex and repetitive code

Java Stream is a declarative interface that is designed for expressing what you want, rather than how to obtain that result. As a result, your queries can be more terse while remaining expressive and intuitive to read.


Stick with a single language


With JPAstreamer, queries are expressed in pure Java. Hence you can avoid any impedances mismatches between JPQL/HQL and Java. 


User.class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@Entity

@Table(name = "user", schema = "mysql")

public class User implements Serializable {


  @Id

  @Column(name = "user_id", nullable = false)

  private Integer userId;


  @Column(name = "name", nullable = false)

  private String name;


  @Column(name = "age")

  private Integer age;


}

JPA Entities as the starting point


JPAstreamer relies on existing JPA entities to generate static metamodel classes needed for typesafe Stream parameters. Upon compilation, an annotation processor generates predicate field builders for all classes annotated with @Entity.

SpringEntryPoint.class (for Spring users)

1

2

@Autowired
private final JPAStreamer jpaStreamer;

Main.class (general initiation)

1

var jpaStreamer = JPAStreamer.of("mysql");

Simple initiation


JPAstreamer is generally initiated with a builder. Although, Spring users have the option to use dependency injection with the @Autowired annotation.

Main.class

1

2

3

4

5

6

jpaStreamer.stream(User.class)

    .filter(User$.age.equal("22"))

    .sorted(User$.name.reversed())

    .skip(10)

    .limit(5)

    .forEach(System.out::println)

Get a Stream from your Bean


Stream any @Entity and apply a custom pipeline to fetch relevant data. Predicates are expressed using the generated field builder, in this case User$. The actual data fetching is delegated to the JPA provider.

Stream your data effortlessly.

Features

Single dependency

Just add a single dependency to your build and start streaming. No configuration needed.

JPA Integration

Integrates seamlessly with any existing JPA provider e.g. Hibernate, Spring Data, and TopLink.

JPMS Compatible

Compatible with the Java Platform Module System (JPMS) for lightweight installation.

Standard Java API

The Java Stream API is an integral part of the Java language since Java 8 that has received praise for offering a declarative and expressive code style. 

API Extension

Enriches the API of JPA providers to include standard Java Stream as a way of expressing queries without impact on the existing application.

Self-optimizing Streams

A custom implementation of the Stream API allows shortcutting of operations to form optimal JPA queries. 

Latest Articles

Express Hibernate Queries as Java Streams 

by Julia Gustafsson

Writing Hibernate queries using the Criteria API can be anything but intuitive and comes at the expense of wordiness. In this article, you will learn how the JPAStreamer Quarkus extension facilitates intuitive and type-safe Hibernate queries...


Continue Reading >> 

Writing Hibernate queries using the Criteria API can be anything but intuitive and comes at the expense of wordiness. In this article, you will learn how the JPAStreamer Quarkus extension facilitates intuitive and type-safe Hibernate queries...


Continue Reading >> 

For the past 10 or so years, JPA has been one of the most popular ways of accessing a database within the Java ecosystem. Even if you haven’t heard of JPA directly, there is a high likelihood that you’ve heard of  Hibernate...


Continue Reading >> 

For the past 10 or so years, JPA has been one of the most popular ways of accessing a database within the Java ecosystem. Even if you haven’t heard of JPA directly, there is a high likelihood that you’ve heard of  Hibernate...


Continue Reading >>