将字符串转换为Hibernate顺序条件中的int [英] Convert string to int in Hibernate order criterion

查看:190
本文介绍了将字符串转换为Hibernate顺序条件中的int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的问题与此相同,但对于Java(JBoss)Hibernate:



我想创建一个列从字符串列转换为int的顺序限制。类似于

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

例外是无法解析属性:cast [id为int] of [Class]。我已经尝试了使用int和integer来转换(as)和convert(,)。

解决方案

尽管话题很老,可能是问题解决了,我会解决。也许这对未来会有帮助。

  criteria.addOrder(new org.hibernate.criterion.Order(anystring,true){
@Override
public String toSqlString(Criteria criteria,CriteriaQuery criteriaQuery)抛出HibernateException {
returncast(id as int);
}
});


Basically, my question is the same as this one, but for Java's (JBoss) Hibernate: How can we order a column as int using hibernate criteria API?

I want to create an order restriction with a cast to int from a string column. Something like

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

The exception is "Could not resolve property: cast(id as int) of [Class]". I've tried both cast( as ) and convert(,) with int and integer.

解决方案

Despite the topic is old and may be the problem were solved, I'll post solution. Maybe it will be helpful in 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)";
            }
        });

这篇关于将字符串转换为Hibernate顺序条件中的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆