我有两个表:CompanyRating
和RatingMeasure
CompanyRating
将与RatingMeasure
进行一对多映射
这是我的域类:
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;
}
}
相应的RatingMeasure表-RATING_MEASURE具有复合主键。 键(measureID)之一是自动生成的。
但是,当我尝试保存时,出现以下错误:
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.measures.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]]
我在做什么错?我期望
measureID
自动分配 最佳答案
是否真的需要在此处使用复合ID?如果您在带有hilo生成器的measureId中使用它,则它已经是唯一的,并且是pk的不错选择,因此我认为不需要复合键。