Bas
Bas

Reputation: 23

Grails additional columns in table or list

I'm trying for several days to receive a list from my Data. The Domain looks like this:

class Alpha {    
    String a
    String b
    etc.
    static hasMany = [beta:Beta]

}

class Beta {
    String a
    Integer foo
    String status
    static belongsTo = [alpha:Alpha]
    static constraints = {
        status(nullable:false, inList:["val1","val2","val3", "val4"])
    }
}

I'd like to have in Alpha the sum of all Beta.foo and of all Beta.foo in a certain status. Best would be something like an additional row ( Integer sumVal1 ... ).

I tried named queries:

static namedQueries = {
        erledigterProbeAufwend {
            createAlias ('Beta', 'b')
            eq ('b.status', 'val1')
            projections {
                groupProperty('b.alpha')
                sum('b.foo', 'sumFooVal1')
            }
        }
}

But this just give me one sum at a time.

I'm looking forward to get some help on that.

Greetings Bas

Upvotes: 2

Views: 1296

Answers (2)

Victor Sergienko
Victor Sergienko

Reputation: 13495

This could be calculated formula field, but with a subquery trick:

static mapping = {
    betaCount formula: "(SELECT count(*) FROM Beta b WHERE b.alpha_id = id and b.status in('a', 'b'))"
}

Upvotes: 3

atodd
atodd

Reputation: 366

Create transient variables in your Alpha class and populate them in an onLoad event.

class Alpha {    
    String a
    String b
    etc.
    static transients = ["sumVal1",...]
    static hasMany = [beta:Beta]

    def onLoad = {
       sumVal1 = ....
    }    

}

Upvotes: 0

Related Questions