Hi,
I'm Thorben Janssen from thoughts-on-java.org with a new Hibernate Tip.
I got the following question via email and thought
that it is an interesting topic for a Hibernate Tip.
So here it is:
I saw JPQL queries using JOIN,
LEFT JOIN and JOIN FETCH clauses
What are the differences between these 3 join clauses?
You might know the JOIN and LEFT JOIN clauses from SQL.
It supports clauses with the same name
and a very similar syntax.
The JOIN FETCH clause is specific to JPA
Let's take a closer look at all 3 options.
Let's start with a simple JOIN clause.
In JPQL, you can define a JOIN clause,
based on a specified association between 2 entities.
Your persistence provider, for example, Hibernate,
translates this into an SQL JOIN clause.
The SQL JOIN clause tells the database
to combine the columns of 2 tables
to a set which you can use within the same query.
You can define a join condition that specifies which rows
of each table shall be joined
with each other.
All rows which don't fulfill the condition are not part of the set.
In most cases, this condition compares the
values of a primary key column of one table
with the values of a foreign key column of an associated table.
But you can also define more complex conditions
using multiple columns of both tables.
In JPQL, you need to define join clauses
based on association mappings.
This mapping provides the names of the foreign key and primary key columns.
That makes the definition of the join clause easier,
but you can't add any additional predicates.
Here's an example of a JPQL query that returns all Author entities
who've written a Book
which title contains the word "Hibernate".
It joins the Author entity with the Book entity
and uses the title attribute of the Book
in the WHERE clause.
If you activate the logging of SQL statements,
you can see that Hibernate generates the
following statement for the JPQL query.
It uses the defined many-to-many association
to join the Author table with the
association table BookAuthor.
It then joins the association table with the Book table.
The LEFT JOIN clause is similar to the JOIN clause.
The main difference is that a LEFT JOIN clause includes all rows of the entity
or table referenced on the left side
of the clause.
I use a LEFT JOIN clause in this query
to select all Authors with the lastName "Janssen"
and their Books.
If the database contains a Book for a specific Author,
the query returns it as the
second element in the Object array.
Otherwise, that array element is null.
A simple JOIN clause would only return the Authors
who have written a Book.
The second element of the Object array
would never be null.
Hibernate generates the following SQL statement
for this query.
It selects all columns mapped by the Author
and Book entities
and uses the defined association to create a left join
between the Book and the Author tables.
The FETCH keyword of the JOIN FETCH clause is JPA-specific.
It tells the persistence provider to not only
join the 2 database tables within the query
but to also initialize the association on the returned entity.
You can use it with a JOIN and a LEFT JOIN statement.
Let's change the first example and replace the
JOIN clause with a JOIN FETCH clause.
The JPQL query selects Author entities.
But as you can see in the SQL statement,
Hibernate now selects all columns mapped by the Author
and the Book entity.
Hibernate then maps the result set to Author and Book entities.
It uses the Book entities to initialize the
books attribute of each Author entity
before it returns a List of Author entities.
OK, that's it for today.
If you want to learn more about Hibernate,
you should join the free Thoughts on Java Library.
It gives you free access to a lot of member-only content
like an ebook about the Java 8 support in Hibernate,
a printable PDF version of this Hibernate Tip,
lots of cheat sheets and a video course.
I'll add the link to it to the video description below.
See you next week for a new Hibernate Tip
and if you like today's video,
please give it thumbs up and subscribe below.
Bye
For more infomation >> What Is the Difference between Laxatives and Stool Softeners? - Duration: 2:36. 




Không có nhận xét nào:
Đăng nhận xét