2012-03-28 18 views
0

의 getter 메소드를 추가 한 후 나는 사용자 이름은 도메인 클래스의 속성 내 사용자 도메인 클래스오류 도메인 클래스

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 

이 왜 세터를 찾고 ? 여기서 내가 뭘 잘못하고 있니? 미리 감사드립니다.

+0

나는이 문제를 발견 .. 아마도 내가 그것을 GET 후 속성을 찾습니다 그렇지 않으면 얻을로 시작하지 않는 함수의 이름을 지정해야합니다. –

답변

2

2.0에서는 문제가 없지만 이전 버전에서는 문제가 없습니다. Groovy 클래스 (예 : 도메인 클래스의 String firstName)에 공용 필드를 추가하면 Groovy 컴파일러가이를 개인 필드와 공용 getter 및 setter로 변환합니다. Grails 도메인 클래스에서 속성은 기본적으로 지속되지만 속성은 일치하는 getter/setter 쌍으로 정의됩니다. 공공 분야가 당신을 위해 이것을 만들 수 있지만 당신은 자바처럼 어려운 방법을 할 수있는 : 당신이 게터 (또는 세터)를 추가 그래서

class Person { 
    private String name 

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

, 그것은 getter/setter 속성 쌍의 절반과 같다 세터는 거기에 없기 때문에, GORM에게는 문제가됩니다.

다행히도 수정 사항은 간단합니다. transients 목록에 게터의 속성 이름을 추가 :

static transients = ['nameAttribute'] 
+0

Burt에게 감사드립니다. 예, 현재 grails 1.3.7을 사용 중이며 곧 2.0으로 업그레이드 할 계획입니다. 지금은 감사를 위해 모든 도메인 클래스에서 공통적 인 함수가 필요하므로 모든 도메인에서 nameattributecall()이라는 이름을 지정하고 해당 도메인의 해당 '이름'필드를 반환합니다. 그래서 함수 호출은 모든 도메인에서 commmon입니다 ... –