Join in Java Persistence with Spring Data and Hibernate
In Java Persistence API (JPA) and Hibernate, a join is a fundamental concept used to associate two entities based on a relationship. Joins are crucial for navigating and querying related entities, allowing developers to combine data from multiple tables based on related columns. This can be implemented using join columns or join tables, depending on the type of association, such as one-to-one, one-to-many, many-to-one, or many-to-many.
Types of Joins
Table Per Subclass with Joins
In the table-per-subclass with joins strategy, joins are used to link subclass tables with the superclass table. When retrieving subclass instances, Hibernate joins the subclass table with the superclass table using the primary key, which also serves as a foreign key. This approach allows for normalization of the SQL schema and facilitates straightforward schema evolution.
Querydsl Joins
In Querydsl, joins are performed using methods like innerJoin, leftJoin, and outerJoin. The on method is used to declare the condition to join on, enabling complex queries involving multiple tables. This is particularly useful for building dynamic queries in a type-safe manner.
Example of Working with Joins in Querydsl
The following listing demonstrates how to work with joins in Querydsl:
@Test
void testJoin() {
List<User> users = queryFactory.selectFrom(QUser.user) #1
.innerJoin(QUser.user.bids, QBid.bid) #2
.on(QBid.bid.amount.eq(new BigDecimal("120.00"))) #3
.fetch(); #4
List<User> otherUsers = queryFactory.selectFrom(QUser.user) #5
.innerJoin(QUser.user.bids, QBid.bid) #5
.on(QBid.bid.amount.eq(new BigDecimal("105.00"))) #5
.fetch(); #5
assertAll( #6
() -> assertEquals(2, users.size()), #6
() -> assertEquals(1, otherUsers.size()), #6
() -> assertEquals("burk", otherUsers.get(0).getUsername()) #6
); #6
}
- #1: Start building the query using the
selectFrommethod belonging to theJPAQueryFactoryclass. - #2: Make the inner join to Bids.
- #3: Define the join on condition as a Predicate to have the Bid amount 120.00.
- #4: Fetch the result.
- #5: Create a similar join for the Bids having amount 105.00.
- #6: Verify that the fetched data is as expected.
SQL Joins in Hibernate
Outer Join
Hibernate can rely on an SQL outer join to determine the concrete subclass for a particular row. The following SQL query demonstrates an outer join:
select
BD.ID, BD.OWNER,
CC.EXPMONTH, CC.EXPYEAR, CC.CARDNUMBER,
BA.ACCOUNT, BA.BANKNAME, BA.SWIFT,
case
when CC.CREDITCARD_ID is not null then 1
when BA.ID is not null then 2
when BD.ID is not null then 0
end
from
BILLINGDETAILS BD
left outer join CREDITCARD CC on BD.ID=CC.CREDITCARD_ID
left outer join BANKACCOUNT BA on BD.ID=BA.ID
The SQL CASE . . . WHEN clause detects the existence (or absence) of rows in the subclass tables CREDITCARD and BANKACCOUNT.
Inner Join
Hibernate also uses inner joins, as shown in the following SQL query:
select
CREDITCARD_ID, OWNER, EXPMONTH, EXPYEAR, CARDNUMBER
from
CREDITCARD
inner join BILLINGDETAILS on CREDITCARD_ID=ID
This mapping strategy can be more complex to implement manually, especially when mixing Spring Data JPA or Hibernate code with handwritten SQL. A common approach is to use JPQL (Jakarta Persistence Query Language) and annotate methods with JPQL queries for a more portable solution.
FAQ (Frequently asked questions)
What is the table-per-subclass with joins strategy in Hibernate?
How are joins performed in Querydsl?
How can you perform a join in Querydsl?
What is the purpose of the on method in Querydsl joins?
What methods are used for working with joins in Querydsl?
How are joins used in JPA to associate entities?