我最近将 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/