从Grails 2.2.4迁移到2.4.3后,在条件查询中使用该属性时,具有名为applicationContext的属性的域类会抛出异常。
class WebResource {
String applicationContext
}
class ResourceService {
public WebResource getResourceByContext(String ctx) {
WebResource.withCriteria() {
eq('applicationContext', ctx)
}
}
}
对getResourceByContext()的调用会抛出异常:
org.hibernate.QueryException: could not resolve property: applicationContext of: org.mydomain.WebResource at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83) at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77) at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1995) at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:61) at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1970) at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:518) at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:534) at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:82) at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:417) at org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:123) at org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:92) at org.hibernate.loader.criteria.CriteriaLoader.(CriteriaLoader.java:97) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1663) at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380) at org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateCriteriaBuilder.invokeMethod(AbstractHibernateCriteriaBuilder.java:1639) at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:907) at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:884) at org.grails.datastore.gorm.GormStaticApi$_withCriteria_closure11.doCall(GormStaticApi.groovy:304) at sun.reflect.GeneratedMethodAccessor254.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207) at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1110) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016) at groovy.lang.Closure.call(Closure.java:423) at org.codehaus.groovy.runtime.ConvertedClosure.invokeCustom(ConvertedClosure.java:51) at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:81) at com.sun.proxy.$Proxy29.doInSession(Unknown Source) 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:303) at org.mydomain.WebResource.withCriteria(WebResource.groovy)
拥有名称为' applicationContext'的域名属性Grails 2.4.3中不允许这样做?我无法在Grails发行说明,升级指南或JIRA中找到对此方案的引用。我通过重命名字段并将GORM引用更改为新名称来解决此问题:
class WebResource {
String appContext
appContext column: 'APPLICATION_CONTEXT'
public String getApplicationContext() {
return appContext
}
public void setApplicationContext(String c) {
appContext = c
}
}
答案 0 :(得分:1)
鉴于Spring ApplicationContext的名称冲突,我怀疑你已经找到了最好的答案 - 将属性命名为其他内容并使用mapping
定义来设置数据库列名。我建议你不要添加getter / setter对,但只需在代码中直接使用属性名appContext
,因为Grails核心或插件中有很多可能的位置(在我看来相当合理)一个名为applicationContext
的属性是Spring对象而不是其他任何东西。
除非必须,否则不要与公约作斗争。