我正在尝试使用表进行映射的域,该表是不具有id列的旧数据库的一部分,因此我选择将String 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)
}
}
这是 Controller
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 }
}
}
}
任何想法如何解决这个问题?
最佳答案
当您决定对id列使用generator:'assigned'
时,Hibernate允许您自己分配id(请参阅docs)。
在您的 Controller 的save方法中,我想您自己没有显式生成ID(除了这种情况,在params中有一个给出有效值的键ID,我不认为这是有效的)。因此,重定向会出现问题,因为它不知道重定向到的位置,因为ID丢失。
如果您确实要重定向到新的PlanType,则必须确保有适当的ID。或者,您可以像这样重定向到index方法:
redirect action: 'index'
关于spring - 具有字符串ID的映射域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25387880/