{{announcement.body}}
{{announcement.title}}

Java14: Join Database Tables With Java 14's New Record

DZone 's Guide to

Java14: Join Database Tables With Java 14's New Record

Did you know that you can join database tables into a Java Stream with Java 14's preview record feature? Find out how with Speedment Stream ORM.

· Java Zone ·
Free Resource

Java14 Records in Joins

Did you know that you can join database tables into a Java Stream with Java 14's preview record feature? Read this short article and find out how it is done using the Speedment Stream ORM. We will start with how to set up your project.

You may also like: A First Look at Records in Java 14

Setup

Download Java 14. Go to the Speedment Initializer and download your project skeleton (including pom.xml). Modify the following lines in your pom.xml file.

XML
 




xxxxxxxxxx
1
15


 
1
<maven.compiler.source>14</maven.compiler.source>
2
<maven.compiler.target>14</maven.compiler.target>
3
 
           
4
...
5
 
           
6
    <plugin>
7
        <artifactId>maven-compiler-plugin</artifactId>
8
        <version>3.8.1</version>
9
        <configuration>
10
            <release>14</release>
11
            <compilerArgs>
12
                --enable-preview
13
            </compilerArgs>
14
        </configuration>
15
    </plugin>



Make sure that you have the latest version of your ide (e.g. IDEA 2010.1) that supports the new Java 14 features.

Speedment Joins

Speedment allows dynamically JOIN:ed database tables to be consumed as standard Java Streams. In this article, we will use the exemplary Sakila database that contains films, actors, languages etc. Download Sakila here or grab a Docker version here.

Tables, views, and joins can easily be turned into standard Java streams with Speedment. This is how it can look like in Java 14:

Java
 




x


1
var speedment = new SakilaApplicationBuilder()
2
.withPassword("sakila")
3
.withBundle(JoinBundle.class)
4
.build();
5
var joinComponent = speedment.getOrThrow(JoinComponent.class);
6
var films = speedment.getOrThrow(FilmManager.class);
7
// Define a Java 14 "record" that can hold a Film and a Language
8
record FilmLanguage(Film film, Language language) {}
9
var join = joinComponent.from(films.getTableIdentifier())
10
.leftJoinOn(Language.LANGUAGE_ID).equal(Film.LANGUAGE_ID)
11
// Provide the constructor of the Java 14 "record"
12
// to be used to construct Film/Language composites
13
.build(FilmLanguage::new);
14
    join.stream()
15
            .forEach(filmLanguage -> System.out.format(
16
                    "%s is in %s%n",
17
                    filmLanguage.film().getTitle(),
18
                    filmLanguage.language().getName())
19
            );
20
 
           



This will produce the following output:

Java
 




xxxxxxxxxx
1


 
1
ACADEMY DINOSAUR is in English
2
ACE GOLDFINGER is in English
3
ADAPTATION HOLES is in English
4
...



Code Breakdown

The from() method takes the first table we want to use (Film). 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 Film and Language entities where the column Language.LANGUAGE_ID equal Film.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.

Note how the constructor of the record FilmLanguage is provided in the build() method. Note also how a film and language entity can be obtained from the record (e.g. filmLanguage.film()). This is a big improvement over previous Java version where we had to provide rather lengthy custom classes or use tuples with accessor like get0() and get1() rather than the much more descriptive film() and language().

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 14 here.
Download Speedment here.
Read all about the JOIN functionality in the Speedment User's Guide.


Further Reading

Signs of JDK 14 Beginning to Appear

Java14: Records

JDK 14: Records, Text Blocks, and More

Topics:
database ,java ,java14 ,join ,records ,speedment orm ,stream api

Published at DZone with permission of Per-Åke Minborg , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}