grails - 升级 Grails withCriteria 后查询不再工作

标签 grails grails-orm

我最近将 Grails 项目更新到最新的 2.x 版本 2.5.0,以及所有依赖项,包括 Hibernate,现在是 4.3.5.2。

一切都正常工作,即数据库正在写入/检索值, Controller 做他们的事情,Spring 安全工作等。但是,现在使用 withCriteria 的模型查询之一抛出一个奇怪的错误。

这两个域类看起来像这样:

class MyModel1 {
   Integer status
   MyModel2 model2
}

class MyModel2 {
   Integer range1
   Integer range2 
}

然后我有这个方法来执行检查子模型的“范围”字段的查询:
public List<MyModel1> doThing(int low, int hi) {

    return MyModel1.withCriteria {
        eq("status", SomeStatus.FLAGGED)
        model2 {
            between("range1", low, hi)
            between("range2", low*2, hi*3)
        }
    }
}

在升级之前,此代码运行良好。现在它提示以下异常:
could not resolve property: model2_alias0 of: com.example.MyModel2. Stacktrace follows:
org.hibernate.QueryException: could not resolve property: model2_alias0 of: com.example.MyModel2
    at grails.gorm.CriteriaBuilder.invokeMethod(CriteriaBuilder.java:329)
    at org.grails.datastore.gorm.GormStaticApi$_withCriteria_closure11.doCall(GormStaticApi.groovy:305)
    at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302)
    at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:37)
    at org.grails.datastore.gorm.GormStaticApi.withCriteria(GormStaticApi.groovy:304)
    at com.example.MyService.doThing(MyService.groovy:42)

(此外,我正在使用 IntelliJ,当我突出显示上述 between() block 中的 withCriteria 语句时,IntelliJ 通知我它“无法解析符号'之间'”。可能不相关,IDK。)

最佳答案

所以看来问题出在我更新到的 Hibernate 版本上。

线索在2.5.0 GORM docs它指出:

Under the hood it uses Hibernate 3 ...



如上所述,我试图使用 v4。我切换回最新版本的3。将以下内容放入 BuildConfig.groovy :
plugins {
   runtime ":hibernate:3.6.10.19"
}

还必须在 中切换此行数据源.groovy :
hibernate {
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // had been this: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'
}

之后,一切正常。 (此外,IntelliJ 仍然提示 between 子句,因此显然不相关。)

关于grails - 升级 Grails withCriteria 后查询不再工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30281048/

相关文章:

grails - 闭包中的变量以避免多个 if

mysql - Grails:我在哪里处理可以有两种不同表示的域类属性?

hibernate - Grails 域类查询 - 如何通过链接到另一个域类的字段进行查找?

grails - 错误映射。DefaultUrlMappingEvaluator$UrlMappingBuilder - 值为 [(*)] 的 URL 映射参数 [异常] 必须是有效的类

grails - 无法将类 'java.lang.String' 的对象 jar 转换为类 'java.util.Map' 。从 grails 3.0.10 升级到 3.1.11 时

grails - 如何在 Grails 中创建动态域类

hibernate - 带有嵌入对象列表的 Grails 2.5.3 域类

html - 如何获取 Angular 的grails数组

unit-testing - 测试Grails服务时创建域对象

grails - 名为 'mailService'的bean应该是 'grails.plugins.mail.MailService'类型,但实际上是 'eafya.MailService'类型