• Stars
    star
    338
  • Rank 124,916 (Top 3 %)
  • Language
    Java
  • License
    GNU Lesser Genera...
  • Created over 4 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JPAstreamer is a lightweight library for expressing JPA queries as Java Streams

JPAstreamer

JPAstreamer Logo

badge badge License LGPL%20v2.1 blue Join%20Chat

JPAstreamer is a lightweight extension for any JPA provider that allows creation of Java Streams from database content.With a single dependency, your application can immediately operate on database elements using standard Stream operators e.g. filter(), sort() and map().

The following example assumes there is an existing JPA Entity that represents a table containing films as follows:

@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.of("sakila");

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

jpaStreamer.stream(Film.class) // Film.class is the @Entity representing the film-table
    .filter(Film$.rating.equal("G"))
    .sorted(Film$.length.reversed().thenComparing(Film$.title.comparator()))
    .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).

In order to minimize the burden on the JVM and the database, JPAstreamer eventually renders the Stream to SQL like so (example from Hibernate/MySQL):

select
    film0_.film_id as film_id1_1_,
    film0_.description as descript2_1_,
    film0_.language_id as languag11_1_,
    film0_.last_update as last_upd3_1_,
    film0_.length as length4_1_,
    film0_.rating as rating5_1_,
    film0_.rental_duration as rental_d6_1_,
    film0_.rental_rate as rental_r7_1_,
    film0_.replacement_cost as replacem8_1_,
    film0_.special_features as special_9_1_,
    film0_.title as title10_1_
from
    film film0_
where
    film0_.rating=?
order by
    film0_.length desc,
    film0_.title asc
limit ?, ?
["G", 10, 5]

More examples are available in the JPAstreamer documentation.

Install

Since JPAstreamer acts merely as an API extension for existing JPA providers it requires minimal installation and configuration efforts. You only need to specify that the JPAstreamer dependency is required to compile your source code.

Note
JPAStreamer requires the use of Java 8 or later.

Maven

In Maven, the following JPAstreamer dependency is added to the project’s pom.xml-file.

<dependencies>
    <dependency>
        <groupId>com.speedment.jpastreamer</groupId>
        <artifactId>jpastreamer-core</artifactId>
        <version>${jpa-streamer-version}</version>
    </dependency>
</dependencies>


<plugins>
    <!-- Needed by some IDEs -->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>add-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>${project.build.directory}/generated-sources/annotations</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

Gradle

In Gradle, the following JPAstreamer dependency is added to the project’s build.gradle-file (replace "version" with the latest JPAstremer version):

repositories {
    mavenCentral()
}

dependencies {
    compile "com.speedment.jpastreamer:jpastreamer-core:version"
    annotationProcessor "com.speedment.jpastreamer:fieldgenerator-standard:version"
}

/* Needed by some IDEs */
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir 'target/generated-sources/annotations'
        }
    }
}

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.

Phone Home

JPAstreamer sends certain data back to servers as described here. If you wish to disable this feature, please contact us at [email protected].

License

JPAstreamer is released under the LGPL 2.1 License.

Short Video

Here is a short 1-minute video describing JPAstreamer:

video tn

More Repositories

1

speedment

Speedment is a Stream ORM Java Toolkit and Runtime
Java
2,091
star
2

hol-streams

This project contains the source code for a hands on lab on Streams
Java
49
star
3

eventsourcing-examples

Examples on how to use Speedment in a Micro-Service environment. Event Sourcing with CQRS and Materialized Views.
Java
18
star
4

freeCodeCamp-tutorial

Contains resources for the Quarkus and JPAStreamer tutorial on freeCodeCamp.
Java
15
star
5

speedment-code-samples

Code samples for the Speedment ORM
Java
13
star
6

employees-webapp

CSS
10
star
7

jpa-streamer-demo

JPAstreamer Demo Project
HTML
9
star
8

avro-mocker

Generate mock data based on an Apache Avro schema and specific cardinality settings
Java
9
star
9

speedment-gradle-plugin

Speedment Gradle Plugin
Java
6
star
10

user-guide-code-samples

Here are the code samples used in the Speedment User's Guide
Java
5
star
11

oracle-java-magazine-examples

This repo contains Speedment examples from the article in Oracle Java Magazine
Java
4
star
12

speedment-doc

The documentation for Speedment
CSS
3
star
13

javaone2017-streams

Repository for the Speedment workshop "Database Actions with Java 9 Stream Syntax Instead of SQL" at JavaOne 2017
Java
3
star
14

speedment-com

The 2017 www.speedment.com website.
PHP
2
star
15

support

Support Repository
2
star
16

generate-speedment-sources

A command-line program that generates repeatable classes and interfaces for the Speedment Open Source project.
Java
2
star
17

speedment-doc-examples

Code examples from the Speedment documentations
Java
1
star
18

db-streams-the-sequel

This repo contains the example used in the article series "Database Streams, the Sequel"
Java
1
star
19

webinar-20171108

Repository for the webinar on November 8th, 2017.
Java
1
star
20

jpastreamer-quarkus-demo

Quarkus application with Hibernate, Panache and JPAStreamer.
HTML
1
star
21

aggregate-offheap

A demonstration of how to aggregate data off-heap
Java
1
star
22

javaone2017-bridging

Java
1
star
23

sakila-webapp

CSS
1
star
24

speedment-resources

This project holds resources such as pictures for web reference.
1
star
25

speedmentversion-maven-plugin

A Maven Plugin that Controls the version in the Speedment project
Java
1
star