grails - 为什么我在保存方法中收到此 ValidationException?

标签 grails grails-orm

我有一个 CourseOffering 域类,它具有 Term 类型的属性,这是另一个域类。

类(class)设置

class CourseOffering {

    static fetchMode = [term: 'eager']

    String title
    String key 
    Term term
}

术语

class Term {
    String description
    String identifier
    String isCurrent
    Date midterm
}

我有一个页面来创建新的 CourseOffering,这是该页面的 gsp。编辑:忘记提及我在这里使用的变量“terms”只是 Term.list() 的传入值

<g:form name="myForm">
    <div class="input">
        Course Title <input type="text" name="title">
        <br><br>
        Course Key  <input type="text" name="key"> 
        <br><br>
        Term <select name="term">
                <g:each var="term" in="${terms}">
                    <option value="${term.id}" <g:if test="${term.isCurrent ==  'Y'}">selected</g:if>>${term.description}</option>
                </g:each>
        </select>
        <br><br>
        <g:actionSubmit action="save" value="Create" />
    </div>
</g:form>

这是 CourseOfferingController 中的保存方法

def save() {
    CourseOffering newCourse = new CourseOffering(params)
    Term term = Term.get(params.term)
    newCourse.term = term
    assert(newCourse.term instanceof Term) // I put this here to make sure it was a Term!

    if (newCourse.save(failOnError: true)) {
        flash.message = [type: 'status', text: "Succesfully added ${newCourse.title} to the list of course offerings."]
    }
    else {
        flash.message = [type: 'errors', text:"Unable to add course to the list of course offerings."]
    }

    redirect(action: 'index')
}

Aaaannnddddd 这是我尝试保存新创建的 CourseOffering 时发生的情况

URI: /office/courseOffering/index Class: grails.validation.ValidationException Message: Validation Error(s) occurred during save(): - Field error in object 'mcidc.CourseOffering' on field 'term': rejected value [mcidc.Term : 9618]; codes [typeMismatch.mcidc.CourseOffering.term,typeMismatch.term,typeMismatch.mcidc.Term,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [mcidc.CourseOffering.term,term]; arguments []; default message [term]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'mcidc.Term' for property 'term'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [mcidc.Term] for property 'term': no matching editors or conversion strategy found

Around line 73 of grails-app\controllers\mcidc\CourseOfferingController.groovy 70: newCourse.term = term 71: assert(newCourse.term instanceof Term) 72:
73: if (newCourse.save(failOnError: true)) { 74: flash.message = [type: 'status', text: "Succesfully added ${newCourse.title} to the list of course offerings."] 75: } 76: else {

这是怎么回事?我用断言语句证明了 newCourse.term 是 Term 类型,那么为什么它说它是一个 String 呢?我很困惑。

很抱歉该堆栈跟踪的格式很糟糕,但我真的很纠结这里的格式,而且我不知道如何让它看起来不错。我试图只发布它的照片,但我没有 10 声望。异常发生在 newCourse.save(failOnError: true)

最佳答案

问题出在这一行:

CourseOffering newCourse = new CourseOffering(params)

因为您通过传入参数创建了 newCourse,所以您正在绑定(bind)参数“term”,而该参数无法将字符串转换为一个。

term 参数重命名为 termId,这将使您继续前进。

普惠制 View :

Term <select name="termId">
                <g:each var="term" in="${terms}">
                    <option value="${term.id}" <g:if test="${term.isCurrent ==  'Y'}">selected</g:if>>${term.description}</option>
                </g:each>
        </select>

Controller :

def save() {
    CourseOffering newCourse = new CourseOffering(params)
    Term term = Term.get(params.termId)
    ...
}

关于grails - 为什么我在保存方法中收到此 ValidationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788110/

相关文章:

hibernate - 在grails中将阿拉伯字符串插入数据库

grails - 多选多选

mysql - 使用 gorm 删除数据库中的所有实体,同时忽略外键约束

java - 更新查询期间 long 类型乘以 double 类型(HQL)

Grails ClassPath 错误,更新到 3.2.1 时

sql - 如果grails中的Hibernate查询中的条件如何应用

java - 增加基于 Windows 安装程序的 Tomcat PermGen Space

database - Grails 自动约束更新

inheritance - GORM 关系中的抽象类

grails - 如何搜索对另一个域类实例的引用为空或与给定值匹配的域类实例?