Convert string to int in sleep order criteria

Basically, my question is the same as this one, but for Java (JBoss) Hibernate: How can we arrange a column as int using hibernate APIs?

I want to create an order constraint cast in int from a row column. Sort of

criteria.addOrder(Order.asc("cast(id as int)"));

The exception is "Failed to resolve property: cast (id as int) [Class]". I tried both cast (as) and convert (,) with int and integer.

+6
source share
2 answers

Despite the fact that the topic is old and the problem may be solved, I will send a solution. Perhaps this will be useful in the future for someone.

criteria.addOrder(new org.hibernate.criterion.Order("anystring", true) {
            @Override
            public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
                return "cast(id as int)";
            }
        });
+16
source
+3

All Articles