Hibernate HQL: Convert SQL Expression to HQL (with subqueries / joins)

I am very grateful for your help in converting the following SQL statement to a valid HQL expression. I tried for several hours but was unsuccessful:

SELECT * FROM master as m left outer join (select * from child as c where c.id = (select max(d.id) from child as d where d.MasterFk = c.MasterFk) )as b ON m.id = b.MasterFk;


public class Master {
 private Long id;
 private Collection childs;
 ...
}

public Class Child {
  private Long id;
  private Master master;
}

In the hibernation mapping file, I map Teacher to the child using standard SET (-Mapping), and the back-to-back relationship from Child to Master is the Many-to-One association. (It works.)

==> The goal is to request a wizard with only the last child record (= Only ONE Record!) Correctly initialized as the only element in the SET of child elements ...

No matter what I try, I have failed.

Many thanks!

+3
source share
2

, . , , - , . Set, , Hibernate Set.

Hibernate, (, Set) ( , ) , . , , , , -, . Hibernate , , ( , ...).

. , , , , , Set !

:

1. Java

, Java-.

, :

    SELECT m.id, max(c.id) 
    FROM master as m 
    left join m.childs as c 
    group by m.id

java , List<Object[]>.

, ( List<Master>):

    SELECT new Master(m.id, max(c.id)) 
    FROM master as m 
    left join m.childs as c 
    group by m.id

2.

lastChild . , , .

, , . , SQL, HQL. SQL, .

0
0

Source: https://habr.com/ru/post/1718001/


All Articles