0

I've got a field called "count". And now I want to order my table by that field. As I know, COUNT is reserved statement in Hibernate. Is there any approach to order table without renaming? My code:

@Override
public List<Distribution> getListOrderByCount(String userName) {
    List<Distribution> result = (List<Distribution>) sessionFactory
            .getCurrentSession()
            .createQuery(
                    "from Distribution where username=:userName ORDER BY count DESC")
            .setString("userName", userName).list();
    return result;
}
Tony
  • 3,605
  • 14
  • 52
  • 84
  • Try using Criteria Queries or other solution is to sort it yourself... ;) – Fincio Aug 25 '14 at 11:30
  • This might be helpful (similar problem on SO) - http://stackoverflow.com/questions/3364835/automatic-reserved-word-escaping-for-hibernate-tables-and-columns – Lucas Aug 25 '14 at 11:31
  • 1
    You can use Alias to map transformers. Might be helpful: http://stackoverflow.com/questions/5686412/how-to-escape-reserved-words-in-hibernates-hql – Mark Smit Aug 25 '14 at 11:33

1 Answers1

3

Try this !

@Override
public List<Distribution> getListOrderByCount(String userName) {
    List<Distribution> result = (List<Distribution>) sessionFactory
            .getCurrentSession()
            .createQuery(
      "from Distribution as d where d.username=:userName ORDER BY d.count DESC")
            .setString("userName", userName).list();
    return result;
}
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56