使用字符串ID映射域

时间:2014-08-19 15:55:05

标签: spring hibernate grails mapping

我尝试使用表格来映射域,该表格是没有id列的旧数据库的一部分,因此我选择使用字符串ID与现有字符串之一但是,当我尝试创建新实例时,我收到此错误消息。

org.codehaus.groovy.grails.web.servlet.mvc.exceptions.CannotRedirectException 无法重定向对象[PlanType :(未保存)]它不是域或没有标识符。请改用显式重定向

以下是域名:

class PlanType {

static hasMany = [template:Template]

String id
String name
String description
String emailId
String initialPhase
String productType

static mapping = {

version false
table 'PLAN_TYPES'

id           generator:'assigned', name:'name', type: 'string'

name         column: 'PLAN_TYPE' 
emailId      column: 'EMAIL_ID'
initialPhase column: 'INITAL_PHASE'
productType  column: 'PRODUCT_TYPE'

}

static constraints = {
    id           (bindable:true)
    description  (maxSize:100, blank:true, nullable:true)
    emailId      (maxSize:50,  blank:true, nullable:true)
    initialPhase (maxSize:250, blank:true, nullable:true)
    productType  (maxSize:20,  blank:true, nullable:true)
}       
}

这是控制器

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class PlanTypeController {

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond PlanType.list(params), model:[planTypeInstanceCount: PlanType.count()]
}

def show(PlanType planTypeInstance) {
    respond planTypeInstance
}

def create() {
    respond new PlanType(params)
}

@Transactional
def save(PlanType planTypeInstance) {
    if (planTypeInstance == null) {
        notFound()
        return
    }

    if (planTypeInstance.hasErrors()) {
        respond planTypeInstance.errors, view:'create'
        return
    }

    planTypeInstance.save flush:true

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'planTypeInstance.label', default: 'PlanType'), planTypeInstance.id])
            redirect planTypeInstance
        }
        '*' { respond planTypeInstance, [status: CREATED] }
    }
}

def edit(PlanType planTypeInstance) {
    respond planTypeInstance
}

@Transactional
def update(PlanType planTypeInstance) {
    if (planTypeInstance == null) {
        notFound()
        return
    }

    if (planTypeInstance.hasErrors()) {
        respond planTypeInstance.errors, view:'edit'
        return
    }

    planTypeInstance.save flush:true

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.updated.message', args: [message(code: 'PlanType.label', default: 'PlanType'), planTypeInstance.id])
            redirect planTypeInstance
        }
        '*'{ respond planTypeInstance, [status: OK] }
    }
}

@Transactional
def delete(PlanType planTypeInstance) {

    if (planTypeInstance == null) {
        notFound()
        return
    }

    planTypeInstance.delete flush:true

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'PlanType.label', default: 'PlanType'), planTypeInstance.id])
            redirect action:"index", method:"GET"
        }
        '*'{ render status: NO_CONTENT }
    }
}

protected void notFound() {
    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.not.found.message', args:    [message(code: 'planTypeInstance.label', default: 'PlanType'), params.id])
            redirect action: "index", method: "GET"
        }
        '*'{ render status: NOT_FOUND }
    }
}
}

任何想法如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

当你决定使用generator:'assigned'作为id列时,Hibernate允许你自己分配id(参见docs)。

在你的控制器的save方法中,我猜你自己没有明确的id生成(除了这种情况,在params中有一个带有有效值的密钥id,我不认为)。因此,重定向有问题,因为它不知道重定向到哪里,因为id丢失了。

如果你真的想要重定向到新的PlanType,你必须确定,有一个正确的id inplace。或者,您可以像这样重定向到索引方法:

redirect action: 'index'

答案 1 :(得分:0)

因为您有自定义ID,所以需要将其指定为重定向方法。

以下是来自您的控制器的修改后的代码段:

        request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'planTypeInstance.label', default: 'PlanType'), planTypeInstance.name])
            redirect action: "show", id: planTypeInstance.name
        }
        '*' { respond planTypeInstance, [status: CREATED] }
    }

答案 2 :(得分:0)

替换重定向planTypeInstance

重定向控制器:“planType”,id:planTypeInstance.id

在两个地方保存关闭和更新关闭

绝对有效

价:http://grails.1312388.n4.nabble.com/CannotRedirectException-after-update-of-simple-table-td4655841.html