0% found this document useful (0 votes)
37 views1 page

JoinUnrelatedEntities PDF

To join unrelated entities in JPA and Hibernate versions older than 5.1, a query must reference both entities in the FROM clause to create a cartesian product, and then reduce it in the WHERE clause by specifying conditions on matching attributes. Hibernate 5.1 introduced explicit JOIN syntax to perform the same operation without a cartesian product by specifying the join criteria in an ON clause. The document provides examples of how to write queries to join unrelated entities both before and after Hibernate 5.1.

Uploaded by

Adolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views1 page

JoinUnrelatedEntities PDF

To join unrelated entities in JPA and Hibernate versions older than 5.1, a query must reference both entities in the FROM clause to create a cartesian product, and then reduce it in the WHERE clause by specifying conditions on matching attributes. Hibernate 5.1 introduced explicit JOIN syntax to perform the same operation without a cartesian product by specifying the join criteria in an ON clause. The document provides examples of how to write queries to join unrelated entities both before and after Hibernate 5.1.

Uploaded by

Adolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

How To Join Unrelated Entities

JPA and Hibernate older than 5.1


JPA and Hibernate versions older than 5.1 require a defined
relationship to join two entities in a JPQL query. But you can
reference both entities in the FROM part of the query to
create a cartesian product and reduce it in the WHERE part.

em.createQuery(
"SELECT p.firstName, p.lastName, n.phoneNumber “ +
“FROM Person p, PhoneBookEntry n “ +
“WHERE p.firstName = n.firstName “+
“AND p.lastName = n.lastName”
);

Hibernate 5.1
Hibernate 5.1 introduced explicit joins on unrelated.

em.createQuery(
"SELECT p.firstName, p.lastName, n.phoneNumber “ +
“FROM Person p “ +
“JOIN PhoneBookEntry n “ +
“ON p.firstName = n.firstName “ +
“AND p.lastName = n.lastName"
);

www.thoughts-on-java.org

You might also like