DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Java 11: JOIN Tables, Get Java Streams

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.

Per-Åke Minborg user avatar by
Per-Åke Minborg
·
Dec. 05, 18 · Presentation
Like (8)
Save
Tweet
Share
13.21K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Database Java (programming language) Joins (concurrency library) Stream (computing)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Distributed SQL: An Alternative to Database Sharding
  • Web Application Architecture: The Latest Guide
  • Understanding gRPC Concepts, Use Cases, and Best Practices
  • The Future of Cloud Engineering Evolves

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: