1

I am going to sort a table data by a column using database collation.

SELECT *
    FROM country
    ORDER BY code COLLATE utf8_swedish_ci DESC;

But when I am trying to get table data with HQL Its getting exception because HQL does not support this feature. So is there any way to do it same as sql? And although hibernate does not support all sql features but may be there is a logical reason for not supporting collate feature. Why hibernate HQL does not support collate feature in query level?

flopcoder
  • 1,195
  • 12
  • 25

2 Answers2

0

You can do it with FluentJPA:

public List<Country> getCountries() {
    FluentQuery query = FluentJPA.SQL((Country country) -> {

        SELECT(country);
        FROM(country);
        ORDER(BY(COLLATE(country.getCode(), "utf8_swedish_ci")).DESC());
    });

    return query.createQuery(em, Country.class).getListResult();
}
Konstantin Triger
  • 1,576
  • 14
  • 11
0

We can do it by override renderOrderByElement method of database dialect

 @Override
    public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls)
    {
        expression = (new StringBuilder(expression)).append(" collate  'utf8_swedish_ci'").toString();
        return super.renderOrderByElement(expression, collation, order, nulls);
    }
flopcoder
  • 1,195
  • 12
  • 25