userimage

Convert string to int in Hibernate order criterion

I want to get the records in an order along with some restrictions by using hibernate order criterion.

But the order column is a string ,I want to create an order restriction with a cast to int from a string column.

userimage

I hope below code will help you to cast the criteria ordering,



List products= session.createCriteria(Product.class)
    .addOrder(new org.hibernate.criterion.Order("ProductId", true) {
    @Override
    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
          return "cast(ProductId as int)";
    }}).list();
Answer is