2014-04-26 2 views
3

Grails를 처음 사용하고 "has-many"애트리뷰트가 변경되면 약간의 계산이 필요합니다. 내 속성이 addTo 및 removeFrom 메소드에서 가장 좋은 위치에 있어야한다는 것을 고려할 때, 속성의 설정자 인 경우이를 무시하려고했지만 작동하지 않았습니다.Grails에서 애트리뷰트의 변경 사항을 수신하기 위해 addTo와 removeFrom을 오버라이드합니다.

이렇게하는 것이 가장 좋은 방법입니까? 내 코드에 무슨 문제가 있습니까? ,

No signature of method: com.rpc.mock.app.Cicle.addToMeasurements() is applicable for argument types: (com.rpc.mock.app.Measurement) values: [com.rpc.mock.app.Measurement : (unsaved)] 
Possible solutions: addToMeasurements(com.rpc.mock.app.Measurement), addToMeasurements(java.lang.Object), getMeasurements(). Stacktrace follows: 
Message: No signature of method: com.rpc.mock.app.Cicle.addToMeasurements() is applicable for argument types: (com.rpc.mock.app.Measurement) values: [com.rpc.mock.app.Measurement : (unsaved)] 
Possible solutions: addToMeasurements(com.rpc.mock.app.Measurement), addToMeasurements(java.lang.Object), getMeasurements() 
    Line | Method 
->> 16 | addToMeasurements in com.rpc.mock.app.Cicle 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|  43 | $tt__save   in com.rpc.mock.app.MeasurementController 
| 200 | doFilter . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter 
|  63 | doFilter   in grails.plugin.cache.web.filter.AbstractFilter 
| 1145 | runWorker . . . . in java.util.concurrent.ThreadPoolExecutor 
| 615 | run    in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 744 | run . . . . . . . in java.lang.Thread 

감사

답변

0

당신이 도메인 객체를 처리하고 같이 이것은 내가 얻을 예외

Cicle.groovy

class Cicle { 

String machine 
int cicleValue 

static hasMany = [measurements:Measurement] 

static constraints = { 
    machine blank:false 
    cicleValue nullable:false 
} 

public void addToMeasurements(Measurement measurement){ 
    super.addToMeasurements(measurement) 
    updateCalculations() 
} 

public void updateCalculations(){ 

    int sumCicles = 0 

    measurements.each{ measurement -> 
     sumCicles += measurement.cicleValue 
    } 

    cicleValue = sumCicles/measurements.size() 
    this.save(failOnError: true) 
} 
} 

에게 :

여기에 코드입니다 GORM은 d와 같은 특정 이벤트가 발생할 때 시작되는 메소드로 이벤트 등록을 지원합니다. eletes, insertsand 업데이트 : 그럼

beforeInsert - Executed before an object is initially persisted to the database 
beforeUpdate - Executed before an object is updated 
beforeDelete - Executed before an object is deleted 
beforeValidate - Executed before an object is validated 
afterInsert - Executed after an object is persisted to the database 
afterUpdate - Executed after an object has been updated 
afterDelete - Executed after an object has been deleted 
onLoad - Executed when an object is loaded from the database 

, 당신이 그와 같은 도메인 객체에 updateCalculations()를 추가 할 수 있습니다

static constraints = { 
    machine blank:false 
    cicleValue nullable:false 
} 

def beforeUpdate() { updateCalculations() } 

을 일반 좋은 디자인 사례로서,이 도메인 개체에서 로직 구현을 유지하는 것이 좋습니다 및 Grails는 도메인에 서비스를 삽입 할 수 있습니다 (POGO).

+0

예. 더 좋은 디자인입니다. 감사합니다. –