Skip to content

Commit

Permalink
add example of subquery join with 'on' to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Oct 21, 2022
1 parent 058984b commit 78c8a04
Showing 1 changed file with 21 additions and 6 deletions.
Expand Up @@ -3142,16 +3142,31 @@ public void test_hql_derived_join_example() {

doInJPA(this::entityManagerFactory, entityManager -> {
//tag::hql-derived-join-example[]
List<Tuple> calls = entityManager.createQuery(
"select longest.duration " +
List<Tuple> calls1 = entityManager.createQuery(
"from Phone p " +
"left join (" +
" select c.duration as duration, c.phone.id as cid" +
" from Call c" +
" order by c.duration desc" +
" limit 1" +
" ) as longest on cid = p.id " +
"where p.number = :phoneNumber " +
"select longest.duration",
Tuple.class)
.setParameter("phoneNumber", "123-456-7890")
.getResultList();

//same, but using 'join lateral' instead of 'on'
List<Tuple> calls2 = entityManager.createQuery(
"from Phone p " +
"left join lateral (" +
" select c.duration as duration " +
" select c.duration as duration" +
" from p.calls c" +
" order by c.duration desc" +
" limit 1 " +
" ) longest " +
"where p.number = :phoneNumber",
" limit 1" +
" ) as longest " +
"where p.number = :phoneNumber " +
"select longest.duration",
Tuple.class)
.setParameter("phoneNumber", "123-456-7890")
.getResultList();
Expand Down

0 comments on commit 78c8a04

Please sign in to comment.