2015-07-06 35 views
0

子類的行數我有一個看起來像兩個領域類:獲取Grails中

class NumberPattern { 

String name 
String desc 

static hasMany = [dns : NumberPatternDNs] 

static mapping = { 
    table 'ni_regexp_ud_def' 
    version false 
    name column: 'NAME' 
    desc column: 'DESCRIPTION' 
    id column: 'REGEXP_UD_DEF_PK' 
} 

} 

而且

class NumberPatternDNs implements Serializable{ 

String dn 

static belongsTo = [pattern : NumberPattern] 

static mapping = { 
    table 'ni_regexp_ud_match' 
    dn column : 'DN_VALUE' 
    version false 
    pattern column : 'REGEXP_UD_DEF_FK' 
    id composite: ['pattern', 'dn'] 
} 

} 

我現在做NumberPatter.findAll()返回我父類中的所有行和子類(相應的FK值)。 我想要的是,而不是得到子行,我應該只得到行數。 我如何做到這一點?

回答

0

這是不是在你需要什麼格式的行數清楚,所以這裏是一個選項

NumberPattern.findAllByName('name').collect { np -> 
    [numberPattern: np, dnsCount: np.dns.size()] 
} 

,或者,如果你需要在一個地圖中的所有數據:

NumberPattern.findAllByName('name').collect { np -> 
    new HashMap(np.properties) + [dnsCount: np.dns.size()] 
} 

,或者你可以使用hql查詢:

NumberPattern.executeQuery("select np.id, count(dns) " + 
    "from NumberPattern np left join np.dns dns " + 
    "where dns.pattern = np and np.id = xyz group by np")