【发布时间】:2014-10-12 19:36:36
【问题描述】:
我正在尝试使用一个表映射一个域,该表是没有 id 列的遗留数据库的一部分,所以我选择使用带有现有列之一的字符串 id 但是我得到了当我尝试创建新实例时出现此错误消息。
org.codehaus.groovy.grails.web.servlet.mvc.exceptions.CannotRedirectException 无法重定向对象 [PlanType : (unsaved)] 它不是域或没有标识符。改用显式重定向
这是域名:
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 }
}
}
}
有什么办法解决这个问题吗?
【问题讨论】:
标签: spring hibernate grails mapping