开发者

one-to-many mapping in Grails | can I use generated value within composite keys?

I've two tables: CompanyRating and RatingMeasure

CompanyRating will have one-to-many mapping with RatingMeasure

Here are my domain classes:

package com.cedar.ts.bbk

import java.io.Serializable;

class CompanyRating implements Serializable {
    private static final long serialVersionUID = -6435485664486973464L;

    String cmpName
    String bbkRating
    String understanding
    java.sql.Date reportDate
    String analystComments

    List<RatingMeasure> measures = new ArrayList<RatingMeasure>();

    static hasMany = [measures: RatingMeasure]

    static constraints = {
        cmpName(blank:true)
        bbkRating(blank:true)
        understanding(blank:true)
        reportDate(blank:true)
        analystComments(blank:true)
    }

    static mapping = {
        id generator:'hilo', params:[table:'hi_value',column:'next_value',max_lo:100]
        table 'CMP_RATING'
        version false
        cmpName column:'CMPNAME'
        bbkRating column:'BBKRATING'
        understanding column:'UNDERSTANDING'
        reportDate column:'REPORTDATE'
        analystComments column:'ANALYSTCOMMENTS'
    }

    def String toString(){
        return cmpName ; //+ " " + bbkRating +" " + understanding  +" " + analystComments +" " +  measures;
    }
}


package com.cedar.ts.bbk

class RatingMeasure implements Serializable{
    private static final long serialVersionUID = -6435485664486973464L;

    Integer measureID
    String measure
    String keyRatios
    String company
    String targetValue
    String indicatorDirection
    CompanyRating companyRating

    static belongsTo = [companyRating:CompanyRating]

    static constraints = {
        measureID(blank:true)
        measure(blank:true)
        measure(blank:true)
        keyRatios(blank:true)
        company(blank:true)
        targetValue(blank:true)
        indicatorDirection(blank:true)
    }

    static mapping = {
        table 'RATING_MEASURE'
        version false
        measureID column:'MSR_ID'
        measure column:'MEASURE'
        keyRatios column:'KEYRATIOS'
        company column:'COMPANY'
        targetValue column:'TARGETVALUE'
        indicatorDirection column:'INDICATORDIRECTION'
        companyRating column:'CMP_ID'

        measureID generator:'hilo', params:[table:'hi_value',column:'next_value',max_lo:100]
        id composite:['measureID', 'companyRating'], generator: "assigned"
    }

    def String toString(){
        return measure;
    }
}

The corresponding RatingMeasure table - RATING_MEASURE has a composite primary key. One of the key(measureID) is a autogenerated one.

However, when I try to save, am getting the following error:

Property [{0}] of class [{1}] cannot be null
[Field error in object 'com.cedar.ts.bbk.CompanyRating' on field 'measures.measureID': rejected value [null];,nullable.com.cedar.ts.bbk.CompanyRating.measur开发者_如何学JAVAes.measureID,nullable.measures.measureID,nullable.measureID,nullable]; arguments [measureID,class com.cedar.ts.bbk.RatingMeasure]; default message [Property [{0}] of class [{1}] cannot be null]]

What wrong am I doing? I am expecting measureID to be automatically assigned


Is it really necessary to use a composite id here? If you're using measureId with a hilo generator it's already unique and a good choice for the pk, so I don't see the need for a composite key.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜