【问题标题】:After upgrading Grails withCriteria query no longer working升级 Grails withCriteria 后查询不再工作
【发布时间】:2015-05-16 21:40:21
【问题描述】:

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

大部分都可以正常工作,即数据库可以正常写入/检索值,控制器可以正常工作,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,当我在上面的 withCriteria 块中突出显示 between() 语句时,IntelliJ 告诉我它“无法解析符号'之间'”。可能不相关,IDK。)

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

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

    线索在 2.5.0 GORM docs 中,它指出:

    它使用 Hibernate 3 ...

    如上所述,我正在尝试使用 v4。我切换回 3 的最新版本。将以下内容放入 BuildConfig.groovy:

    plugins {
       runtime ":hibernate:3.6.10.19"
    }
    

    还必须在DataSource.groovy中切换这一行:

    hibernate {
        cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // had been this: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'
    }
    

    之后,一切正常。 (此外,IntelliJ 仍然抱怨 between 子句,因此很明显,不相关。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2018-09-16
      • 2012-12-13
      • 2016-01-03
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多