2017  Kodetalk | Feedback | Privacy Policy | Terms | About

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.


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) {
    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
          return "cast(ProductId as int)";
Answer is