Question

I am puzzled by a behavior I'm facing with grails data binding. I will start with the controller code

def saveQuestion(){
   QuestionSurvey question = new QuestionSurvey();
   bindData(question, params);
   question.save(); 
}

Also the QuestionSurvey domain looks like

class QuestionSurvey {

    String questionText
    QuestionTypeSurvey questionType

    static hasMany = [options: QuestionOptionSurvey]

    static mapping = {  
        questionType enumType: 'ordinal'
    }

    static constraints = {}
}

Also, QuestionOptionSurvey domain looks like

class QuestionOptionSurvey {

    String optionText

    static belongsTo = [question: QuestionSurvey]

    static constraints = { }
}

The binding I think is working for all the properties but not the "options" field in QuestionSurvey domain. The error I'm getting is

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?

The params.options is a list of strings for example ['option1', 'option2', 'option2'].

Was it helpful?

Solution

In order to bind the options property/collection correctly your parameters should be posted with element names like: options.optionText. Otherwise it's going to expect you are posting ids and try and locate the QuestionOptionSurvey matching those ids.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top