Well designed databases are normalized to reduce the redundancy. This separates the data in a plethora of tables and you must rely on the JOIN operator to reconnect the dots between the data entries. JOINs will add complexity to the query and the syntax required to express the desired result can become messy and error-prone. In this final part of my Intro to Speedment guide, I will show you how JOINs can be expressed in a type-safe and intuitive manner with Java Streams and Speedment.

Before we can start using Joins, we need to add the JoinBundle to our Application. To do so, we need to add the following line to our ApplicationBuilder:

Java

1

.withBundle(JoinBundle.class) 

This gives us access to a JoinComponent, which is a Speedment component responsible for modeling Joins. We can access it like so: final JoinComponent joinComponent = application.getOrThrow(JoinComponent.class); 

Creating a Simple Join

Creating Joins with Speedment is rather simple. As an example, let’s say that we want to join the store and staff tables based on the id of the store and the store_id column in the staff table.

The JoinComponent contains a single method called from which takes in a table identifier. The table identifier tells Speedment which table to use as the base. In our case that would be the store table. All Managers hold TableIdentifier references to their respective tables, which can be statically accessed by calling Manager#IDENTIFIER.

#java #tutorial #sql #join #speedment #semantic join

Expressing Joins: Intro to Speedment
1.05 GEEK