【问题标题】:Override addTo and removeFrom to listen to changes in an attribute in Grails覆盖 addTo 和 removeFrom 以监听 Grails 中属性的变化
【发布时间】:2014-04-26 19:21:14
【问题描述】:

我是 Grails 的新手,当“具有多个”属性发生更改时,我需要进行一些计算,而且我认为这样做的最佳位置是在属性的设置器中,因为我的属性是一个列表最好的地方应该是 addTo 和 removeFrom 方法,我试图覆盖它们但没有奏效。

那么这是最好的方法吗?我的代码有什么问题?

代码如下:

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

这是我得到的例外:

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

谢谢

【问题讨论】:

    标签: grails groovy model has-many


    【解决方案1】:

    当您处理域对象时,GORM 支持将事件注册为在某些事件发生时触发的方法,例如删除、插入和更新:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-06
      • 2017-05-16
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      相关资源
      最近更新 更多