title: Hibernate how to build sql query?
body: <p>I have the following query in Postgres 11</p>
  
  <pre><code>SELECT DISTINCT ON (account.name) *
  FROM sys_accounts AS account 
    INNER JOIN sys_activities AS activity ON account.guid=activity.account_guid 
  WHERE activity.datefinish IS NOT NULL 
    AND (extract(day from NOW() - activity.datefinish)) &gt; 60 
    AND dc_status_guid='df11a68c-20d3-46e2-840f-2e4548aacf1b'  
  ORDER  BY  account.name,activity.datefinish DESC
  </code></pre>
  
  <p>How can I run that through Hibernate (5.4)</p>
  
  <p>I need get all accounts where last task closed > 60 days ago</p>
  
  <p>Java structure like</p>
  
  <pre><code>Account (oneToMany){
                      Activity1 :finishdate |stage ....
                      Activity2 :finishdate |stage ....
                      ......
                     }
  </code></pre>
  
  <p>I don't understand how to write this request with hibernate syntax HQL or CRITERIA. With HQL i get error:Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ON near line 1, column 17 [SELECT DISTINCT ON (account.name)* FROM sys_accounts AS account INNER JOIN sys_activities AS activity ON account.guid=activity.account_guid WHERE activity.datefinish IS NOT NULL AND dc_status_guid='df11a68c-20d3-46e2-840f-2e4548aacf1b' ORDER BY account.name,activity.datefinish DESC]</p>