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? 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.
Setup
Download Java 14. Go to the Speedment Initializer and download your project skelaton (including pom.xml
). Modify the following lines in yourpom.xml
file:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | < maven.compiler.source >14</ maven.compiler.source > < maven.compiler.target >14</ maven.compiler.target > ... < plugin > < artifactId >maven-compiler-plugin</ artifactId > < version >3.8.1</ version > < configuration > < release >14</ release > < compilerArgs > --enable-preview </ compilerArgs > </ configuration > </ 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:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | var speedment = new SakilaApplicationBuilder() .withPassword( "sakila" ) .withBundle(JoinBundle. class ) .build(); var joinComponent = speedment.getOrThrow(JoinComponent. class ); var films = speedment.getOrThrow(FilmManager. class ); // Define a Java 14 "record" that can hold a Film and a Language record FilmLanguage(Film film, Language language) {} var join = joinComponent.from(films.getTableIdentifier()) .leftJoinOn(Language.LANGUAGE_ID).equal(Film.LANGUAGE_ID) // Provide the constructor of the Java 14 "record" // to be used to construct Film/Language composites .build(FilmLanguage:: new ); join.stream() .forEach(filmLanguage -> System.out.format( "%s is in %s%n" , filmLanguage.film().getTitle(), filmLanguage.language().getName()) ); |
This will produce the following output:
1 2 3 4 | ACADEMY DINOSAUR is in English ACE GOLDFINGER is in English ADAPTATION HOLES is in English ... |
Code Breakdown
The from()
method takes the first table we want to use (Film
). TheinnerJoinOn()
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 thebuild()
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.
Published on Java Code Geeks with permission by Per Minborg, partner at our JCG program. See the original article here: Java14: Join Database Tables with Java 14’s new Record Opinions expressed by Java Code Geeks contributors are their own. |
Hi…
I’m Elena gillbert.Records introduce the capability of properly implementing data classes, without the need to write verbose code. Plain data classes are reduced from several lines of code to a one-liner. There are other language features in progress that work well with records, such as pattern matching. For a much deeper dive into records and background information, see Brian Goetz’s exploratory document on OpenJDK.