Wojciech Pilzak
Wojciech Pilzak

Reputation: 11

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.

Upvotes: 1

Views: 1531

Answers (2)

Dedovich Sergey
Dedovich Sergey

Reputation: 11

There is a solution (SQL Server), f.e.:

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

Upvotes: 1

Wojciech Pilzak
Wojciech Pilzak

Reputation: 11

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?

Upvotes: 0

Related Questions