我对grails数据绑定所面临的行为感到困惑。我将从控制器代码
开始def saveQuestion(){
QuestionSurvey question = new QuestionSurvey();
bindData(question, params);
question.save();
}
QuestionSurvey域名也是
class QuestionSurvey {
String questionText
QuestionTypeSurvey questionType
static hasMany = [options: QuestionOptionSurvey]
static mapping = {
questionType enumType: 'ordinal'
}
static constraints = {}
}
另外,QuestionOptionSurvey域看起来像
class QuestionOptionSurvey {
String optionText
static belongsTo = [question: QuestionSurvey]
static constraints = { }
}
我认为绑定适用于所有属性,但不适用于QuestionSurvey域中的“选项”字段。我得到的错误是
TypeMismatchException: Provided id of the wrong type for class QuestionOptionSurvey.
Expected class java.lang.Long got java.lang.String. So my question is how to get around
this problem of binding params to a domain that has a field of type set of another domain
class?
params.options是一个字符串列表,例如['option1','option2','option2']。
答案 0 :(得分:1)
为了正确绑定options
属性/集合,您的参数应使用以下元素名称发布:options.optionText
。否则,它会期望您发布ID并尝试找到与这些ID匹配的QuestionOptionSurvey
。