简体   繁体   中英

After upgrading Grails withCriteria query no longer working

I recently updated a Grails project to the latest 2.x version, 2.5.0, along with all the dependencies, including Hibernate, which is now at 4.3.5.2.

Everything mostly works, ie the database is writing/retrieving values fine, controllers do their thing, Spring security works, etc. However, now one of the model queries using withCriteria is throwing a weird error.

The two domain classes look something like this:

class MyModel1 {
   Integer status
   MyModel2 model2
}

class MyModel2 {
   Integer range1
   Integer range2 
}

And then I have this method that does a query that checks the "range" fields of the sub-model:

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)
        }
    }
}

Prior to upgrading, this code worked fine. Now it complains with the following exception:

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)

(Additionally, I'm using IntelliJ, and when I highlight the between() statements in the above withCriteria block IntelliJ informs me that it "Cannot resolve symbol 'between'". Maybe not relevant, IDK.)

So it appears that the problem was with the version of Hibernate to which I'd updated.

The clue was in the 2.5.0 GORM docs where it states:

Under the hood it uses Hibernate 3 ...

I was trying to use v4, as I state above. I switched back to the latest version of 3. Put the following in BuildConfig.groovy :

plugins {
   runtime ":hibernate:3.6.10.19"
}

Also had to switch this line in DataSource.groovy :

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

After that, everything works as it should. (Also, IntelliJ still complains about the between clause, so apparently, not relevant.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM