-1

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'].

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.