在更新查询(HQL)期间乘以double类型的long类型

时间:2013-12-16 11:19:43

标签: java sql hibernate hql gorm

我有下一个实体

class DomTestEntity {

    Long completeValue
    Long rawValue

}

我想使用rawValue乘以双数(rate)来更新completeValue。写出简单的方法来做到这一点:

def testHql(){
    Double rate = 0.7
    DomTestEntity.executeUpdate("update DomTestEntity item set item.completeValue = item.rawValue * :rate", [rate: rate])

    render view: "index", model: [message: "Test complete"]
}

但它会引发错误:

java.lang.Double cannot be cast to java.lang.Long. Stacktrace follows: - [org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver]
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long
    at companyname.TestController$$EOQBEOjI.testHql(TestController.groovy:122)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

同时sql查询工作正常:

def testHql(){
    Double rate = 0.7
    sessionFactory.currentSession.createSQLQuery("update dom_test_entity e set e.complete_value = e.raw_value * ${rate}").executeUpdate()

    render view: "index", model: [message: "Test complete"]
}

如何避免在Hql中将double值转换为long?或者如何编写适用于sql查询的正确hql查询?

1 个答案:

答案 0 :(得分:1)

您正在尝试将double值保存到Long列中。这是不允许的。要么必须将'completeValue'的类型更改为Double(意味着适当地更改基础DB列类型),要么将最近的长值存储为结果。