简体   繁体   中英

Hibernate change alias in @Formula

I have a field:

@Formula(value = "(select max(crm_kk_rejestr.data_otrzymania) " +
                   "from crm_kk_rejestr " + 
                   "where crm_kk_rejestr.idkntrh = idkntrh)")
private Date dataOtrzymania;

I want sorted by this field, but I get an error:

ORDER BY column or expression must be in SELECT list in this context.

There is a solution (SQL Server), fe:

private String yourFieldForAlias;

@Formula("( validSQLquery ) as 'yourOwnAlias', NULL")
public String getYourFieldForAlias() {
  return yourFieldForAlias;
}

The generated query will be:

....
 ( validSQLquery ) as 'yourOwnAlias', null as formula0_0_
....

and you need just to:

order by yourOwnAlias

SELECT FIRST 50 {...}


  (SELECT max(crm_kk_rejestr.data_otrzymania)
   FROM crm_kk_rejestr
   WHERE crm_kk_rejestr.idkntrh = kontrahent0_.idkntrh) AS formula62_,
             kontrahent0_.nazwa1 || ' ' || kontrahent0_.nazwa2 AS formula63_
FROM kontrahent0 kontrahent0_
ORDER BY
  (SELECT max(crm_kk_rejestr.data_otrzymania)
   FROM crm_kk_rejestr
   WHERE crm_kk_rejestr.idkntrh = kontrahent0_.idkntrh) DESC

Its not work because this is subselect. If i change subselect to alias "formula62_" all work good.

Its possible change alias to my custom in hibernate?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM