2010-12-20 14 views
2

Ich versuche für mehrere Tage eine Liste von meinen Daten zu erhalten. Die Domain sieht wie folgt aus:Grails zusätzliche Spalten in Tabelle oder Liste

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

ich in Alpha die Summe aller Beta.foo und aller Beta.foo in einem bestimmten Status haben möchten. Am besten wäre so etwas wie eine zusätzliche Zeile (Integer sumVal1 ...).

Ich habe versucht, benannte Abfragen:

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

Aber gib mir nur eine Summe auf einmal.

Ich freue mich darauf, Hilfe zu bekommen.

Grüße Bas

Antwort

3

Diese formula field berechnet werden konnte, aber mit einem subquery trick:

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

Vielen Dank für Ihre schnelle Antwort. Es funktioniert gut :) – Bas

+0

Es funktioniert perfekt !! Ich möchte meinen Fall als ein weiteres Beispiel veröffentlichen: 'totalFilhos formula: '(SELECT count (*) FROM aendimento a WHERE a.atendimento_pai = cod_atendimento)' ' –

+0

und was ist, wenn du kein' count (*) willst ', aber alle Zeilen, mit dem gleichen Trick? –

0

transiente Variablen in der Alpha-Klasse erstellen und in einem onLoad Ereignis füllen.

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

    def onLoad = { 
     sumVal1 = .... 
    }  

} 
+0

Ich bezweifle es in Alpha dann mit besser: 'def getVal1() {{beta.findAll it.status in [ 'a ',' b ']}. sum()} 'und fragt es träge. Beim Abrufen von a.beta wird sowieso eine zusätzliche Abfrage ausgeführt. –

Verwandte Themen