grails - 在域类中添加 getter 方法后出错

标签 grails methods grails-orm

我在我的用户域类中添加了以下方法

   String getNameAttribute(){
       return this.username;
    }

其中用户名是域类中的属性。我正在使用它进行审计日志记录以获取用户名来记录它。

但是当我运行应用程序时,出现以下异常
012-03-27 23:01:17,646 [main] ERROR context.GrailsContextLoader  - Error executing bootstraps: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.PropertyNotFoundException: Could not find a setter for property nameAttribute in class com.gra.register.User
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.PropertyNotFoundException: Could not find a setter for property nameAttribute in class com.gra.register.User
    at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
    at grails.web.container.EmbeddableServer$start.call(Unknown Source)
    at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
    at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
    at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
    at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
    at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
    at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
    at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
    at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
    at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
    at RunApp$_run_closure1.doCall(RunApp.groovy:33)
    at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
    at gant.Gant.withBuildListeners(Gant.groovy:427)
    at gant.Gant.this$2$withBuildListeners(Gant.groovy)
    at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
    at gant.Gant.dispatch(Gant.groovy:415)
    at gant.Gant.this$2$dispatch(Gant.groovy)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.executeTargets(Gant.groovy:590)
    at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.PropertyNotFoundException: Could not find a setter for property nameAttribute in class com.gra.register.User
    ... 23 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.PropertyNotFoundException: Could not find a setter for property nameAttribute in class com.gra.register.User
    ... 23 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property nameAttribute in class com.gra.register.User
    ... 23 more

为什么要找二传手?我在这里做错了什么?
提前致谢。

最佳答案

这在 2.0 中不是问题,但在早期版本中存在。当您将公共(public)字段添加到 Groovy 类(例如域类中的 String firstName)时,Groovy 编译器会将其转换为私有(private)字段以及公共(public) getter 和 setter。在 Grails 域类中,属性默认是持久的,但属性被定义为匹配的 getter/setter 对。公共(public)字段会为您创建它,但您可以像在 Java 中那样用困难的方式来完成它:

class Person {
   private String name

   String getName() { return this.name }
   void setName(String name) { this.name = name }
}

因此,当您添加 getter(或 setter)时,它看起来像是 getter/setter 属性对的一半,并且由于 setter 不存在,这对 GORM 来说是个问题。

幸运的是,修复很简单。将 getter 的属性名称添加到 transients列表:
static transients = ['nameAttribute']

关于grails - 在域类中添加 getter 方法后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901991/

相关文章:

json - 将对象根添加到JSON

java - # 在 Java 方法中起什么作用?

grails - 如何动态地将属性/字段添加到 Grails 中的域类?

oracle - grails 条件查询返回空结果

grails - 自定义新的 Grails Spring Security Core Plugin 登录页面

session - Grails Tomcat 无法序列化 sessionid : groovy. lang.MapWithDefault 的增量请求

使用 Neo4j 托管 Grails 来读取/写入文件

Ruby 将字符串转换为方法名

c# - Math.Cound标签在C#中

grails - removeFrom* 方法应该从数据库中删除子项还是应该只删除父子关系?