In java reactor, r2dbc. I have two tables A, B. I also have repositories for them defined. How can i get data made up of A join B?
I only come up with the following approach: call databaseClient.select from A and consequently in a loop call select from B.
But i want more efficient and reactive way. How to do it?
A joined-table specifies an intermediate result table that is the result of either an inner join or an outer join. The table is derived by applying one of the join operators—INNER, FULL OUTER, LEFT OUTER, or RIGHT OUTER—to its operands. Db2 supports inner joins and outer joins (left, right, and full).
You probably noticed that in the join we just performed, there were only three rows in the joined table. That’s because we performed something called an “inner join”, where R only returns the data frame rows that match up with the other data frame. If you were to visualize this type of join, it would look something like this:
Next, it calls findAll () to fetch all Customer objects from the database. Then it calls findById () to fetch a single Customer by its ID. Finally, it calls findByLastName () to find all customers whose last name is "Bauer". R2DBC is a reactive programming technology.
Importantly, to join two different tables together, you need to make sure you have a column in common between both data sets. This common column is called a “key”, and it should provide a unique identifier for every row. In the case of our data, the “student” column is our key, and it provides a unique number for each student.
TL;DR: Using SQL.
Spring Data's DatabaseClient
is an improved and reactive variant for R2DBC of what JdbcTemplate
is for JDBC. It encapsulates various execution modes, resource management, and exception translation. Its fluent API select/insert/update/delete methods are suitable for simple and flat queries. Everything that goes beyond the provided API is subject to SQL usage.
That being said, the method you're looking for is DatabaseClient.execute(…)
:
DatabaseClient client = …;
client.execute("SELECT person.age, address.street FROM person INNER JOIN address ON person.address = address.id");
The exact same goes for repository @Query
methods.
Calling the database during result processing is a good way to lock up the entire result processing as results are fetched stream-wise. Issuing a query while not all results are fetched yet can exhaust the prefetch buffer of 128 or 256 items and cause your result stream to stuck. Additionally, you're creating an N+1 problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With