Hibernate在@Formula中更改了别名

时间:2017-04-04 10:34:48

标签: java hibernate formula alias

我有一个字段:

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

我希望按此字段排序,但收到错误:

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

2 个答案:

答案 0 :(得分:1)

有一个解决方案(SQL Server),例如:

private String yourFieldForAlias;

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

生成的查询将是:

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

你需要:

order by yourOwnAlias

答案 1 :(得分:0)

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

它不起作用,因为这是次选择。如果我将subselect更改为别名" formula62 _"一切顺利。

它可能在hibernate中更改我的自定义别名吗?