2

I'm using Grails 2.4.4. I've got domain class

class Purchase {
    static hasMany = [dataList:DataByPeriod]
    String name

    Set<DataByPeriod> dataList
    public Set<DataByPeriod> getDataList(){
        dataList?.findAll { (!it.isDeleted) }
    }

    def getPeriodList(){
        this.dataList?.period?.unique()?.sort{it.name}
    }
}

and another two:

class DataByPeriod {
    static belongsTo = [purchase:Purchase, period:Period]
    Long value
}
class Period {
    static hasMany = [dataList:DataByPeriod]
    String name
}

Now I want to receive all periods for given purchaseInstance. If I do:

purchaseInstance?.dataList?.period?.unique()?.sort{it.name}

I'll get correct data (getter for dataList will be called and all records with isDeleted==true will be ignored

But if I I do: purchaseInstance?.getPeriodList() getter will not be called and all records will be shown?

Why does this happens? Why I even cannot change my 'getPeriodList()` method to:

def getPeriodList(){
    this.getDataList()?.period?.unique()?.sort{it.name}
}

It says that 'there is no method getDataList() for class Purchase'.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • 2
    you shouldn't override getters for this, it would conflict with hibernate logic. Introduce another method like `List dataListNotDeleted()` or better a `named-query` instead. – injecteer Aug 29 '16 at 16:41
  • Excellent point - if you return a different value than Hibernate set originally it will cause Hibernate to mark that property as 'dirty' and push all values to the database, even if you only load the instance for its data but you aren't intentionally changing anything. – Burt Beckwith Aug 29 '16 at 17:04
  • Thanks for an advice. But I doubt that problem is in overriding, coz it's working fine if I call it directly (in controller). Also as far as I know this overriding is correct as hasMany statement is Set http://docs.grails.org/latest/ref/Domain%20Classes/hasMany.html – Alexander Stepchkov Aug 29 '16 at 18:28
  • I think it would be a really good idea if you listened to what Burt had to say. I believe that he has forgotten more about this stuff than the rest of us know. Just saying. – Steve Hole Aug 30 '16 at 16:56
  • @injecteer I replaced getter with another method and create transient variable and now it works (had to refactor a bit but luckly not too much). But why I couldn't call my getter from another method in same class? Idea IDE even knows it without running project, but I cannot understand what happens beneath which coz it :( – Alexander Stepchkov Aug 31 '16 at 11:48
  • @AlexanderStepchkov I still don't get the point of having a getter for a clear `formula` case (because there's no point in doing that). show some code on `I couldn't call my getter from another method` – injecteer Aug 31 '16 at 12:18
  • @injecteer I agree with you that getter may be not the best way. I just trying to understand underlaing mechanics. Code is in the end of my topic. I cannot call getter `getDataList()` in `class Purchase` it says `there is no method getDataList() for class Purchase` – Alexander Stepchkov Aug 31 '16 at 13:03

0 Answers0