spring - 具有字符串ID的映射域

标签 spring hibernate grails mapping

我正在尝试使用表进行映射的域,该表是不具有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/

相关文章:

grails - 如何以通用方式在域类上调用GORM方法?

java - Spring aws-cloud SNS http端点确认订阅不起作用

java - H2控制台不显示

java - 如何跳过父类(super class)表的模式生成? Java/JPA/Hibernate/注释/Maven/hbm2ddl

java - spring mvc hibernate 锁定表的行

java - 无法访问 JSP 中列表中对象数组中的每个元素

spring - 如何将 @Valid 与 Spring MVC 的 @RequestBody 参数一起使用?

java - 如何在spring mvc中删除session属性

Grails 2.4.4 bindData - 使用 '_' 作为前缀分隔符而不是 '.' ?

grails - 将更新的Grails 2.4.4更新到2.5.6时遇到的问题