Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java 11: JOIN Tables, Get Java Streams

DZone's Guide to

Java 11: JOIN Tables, Get Java Streams

Let's take a look at a way to treat your database's JOIN tables as a Java stream.

· Java Zone ·
Free Resource

FlexNet Code Aware, a free scan tool for developers. Scan Java, NuGet, and NPM packages for open source security and open source license compliance issues.

Java 11: JOIN Tables, Get Java Streams

Ever wondered how you could turn joined database tables into a Java Stream? Read this short article and find out how it is done using the Speedment Stream ORM. We will start with a Java 8 example and then look into the improvements made with Java 11.

Java 8 and JOINs

Speedment allows dynamically JOIN:ed database tables to be consumed as standard Java Streams. We begin by looking at a solution for Java 8 using the Sakila exemplary database:

    Speedment app = ...;

    JoinComponent joinComponent = app.getOrThrow(JoinComponent.class);

    Join<Tuple2OfNullables<Language, Film>> join = joinComponent
        .from(LanguageManager.IDENTIFIER)
        .innerJoinOn(Film.LANGUAGE_ID).equal(Language.LANGUAGE_ID)
        .build();

        join.stream()
            .forEach(System.out::println);


This will produce the following output (reformatted and shortened for readability):

Tuple2OfNullablesImpl {
    LanguageImpl { languageId = 1, name = English, ... }, 
    FilmImpl { filmId = 1, title = ACADEMY DINOSAUR, ... }
}
Tuple2OfNullablesImpl {
    LanguageImpl { languageId = 1, name = English, ... }, 
    FilmImpl { filmId = 2, title = ACE GOLDFINGER, ... }
}
Tuple2OfNullablesImpl {
    LanguageImpl { languageId = 1, name = English, ... },
    FilmImpl { filmId = 3, title = ADAPTATION HOLES, ... }
}
...


Java 11 and JOINs

In the new Java version 11, there is Local-Variable-Type-Inference (aka var declaration), which makes it even easier to write joinswith Speedment. We do not have to explicitly state the type of the join variable:

    Speedment app = ...;

    JoinComponent joinComponent = app.getOrThrow(JoinComponent.class);

    var join = joinComponent
        .from(LanguageManager.IDENTIFIER)
        .innerJoinOn(Film.LANGUAGE_ID).equal(Language.LANGUAGE_ID)
        .build();

        join.stream()
            .forEach(System.out::println);


Code Breakdown

The from() method takes the first table we want to use (Language). The innerJoinOn() method takes a specific column of the second table we want to join. Then, the equal() method takes a column from the first table that we want to use as our join condition. So, in this example, we will get matched Language and Film entities where the column Film.LANGUAGE_ID equal Language.LANGUAGE_ID.

Finally, build() will construct our Join object that can, in turn, be used to create Java Streams. The Join object can be re-used over and over again.

JOIN Types and Conditions

We can use innerJoinOn()leftJoinOn(), rightJoinOn(), and crossJoin(), and tables can be joined using the conditions equal(), notEqual(), lessThan(), lessOrEqual(), greaterThan(), and lessOrEqual().

What's Next?

Download open-source Java 11 here. Download Speedment here.
Read all about the JOIN functionality in the Speedment User's Guide.

 Scan Java, NuGet, and NPM packages for open source security and license compliance issues. 

Topics:
java ,streams ,joins ,tables ,database ,orm

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}