开发者

Why is get-Property in domain class only usable with Set and findAll?

开发者 https://www.devze.com 2023-02-05 10:47 出处:网络
In Domain class FooReward I added a method 开发者_如何学编程int getQuantity() { FooRewardAssignment.countByReward(this)

In Domain class FooReward I added a method

开发者_如何学编程int getQuantity() {
  FooRewardAssignment.countByReward(this)
}

Usage in GSP should be fooRewardInstance.quantity, but that errors on startup (in bootstrap) with message that it has no setter method found.

If I change it to

Set<FooRewardAssignment> getListOfFoos() {
    FooRewardAssignment.findAllByReward(this)
}

and change the usage in GSP to be badeRewardInstance.listOfFoos.size(), it works and shows me how often a special FooReward is assigned.

Any idea what is wrong in version 1?


Two workarounds for this problem:

  1. Changing the return value to def:

    def getQuantity() {    
       FooRewardAssignment.countByReward(this) 
    }
  2. Or by adding transients list:

    static transients = ['quantity'] 
    int getQuantity() {
       FooRewardAssignment.countByReward(this) 
    }

Sometimes GORM create column in tabel on the basis of setters and getters method inside domain class. And in this situation GORM want add column like 'quantity' but the error occur because GORM see only getter, and don't see setter. So we can say that we don't want create this column in database (static transients) or we can set return value as def - but don't ask me why 'def getters' are not taken into account by GORM ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号