gpt4 book ai didi

grails - 保存时发生Grails DB错误(没有域或没有标识符。)

转载 作者:行者123 更新时间:2023-12-02 14:22:25 25 4
gpt4 key购买 nike

我正在开发一个新应用程序,该应用程序需要读取旧版数据库中的现有数据库表。为此,我也必须在开发环境中使用它。但是,当我尝试创建新记录时,它失败并显示以下消息:

URI
/roleType/save
Class
grails.web.mapping.mvc.exceptions.CannotRedirectException
Message
null
Caused by
Cannot redirect for object [com.mytrading.legacy.RoleType : (unsaved)] it is not a domain or has no identifier. Use an explicit redirect instead

为了获得 Controller 和 View ,我运行了“grails generate-all”。

为了清楚起见,在其中删除了一些字段的域看起来像这样:
class RoleType {
int roleType

static mapping = {
table 'RoleType'
version false
id name: 'roleType', type:'int', generator:'assigned'
roleType column: 'RoleType'

}
}

我不知道它们的含义:“不是域或没有标识符”,它们对于显式重定向的含义是什么,我应该重定向到什么?那是唯一的解决方案-我不敢相信。

Controller :
import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import grails.plugin.springsecurity.annotation.Secured

@Secured(['ROLE_ADMIN','ROLE_SALES'])

@Transactional(readOnly = true)
class RoleTypeController {

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

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

def show(RoleType roleType) {
respond roleType
}

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

@Transactional
def save(RoleType roleType) {
if (roleType == null) {
transactionStatus.setRollbackOnly()
notFound()
return
}

if (roleType.hasErrors()) {
transactionStatus.setRollbackOnly()
respond roleType.errors, view:'create'
return
}

roleType.save flush:true

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

def edit(RoleType roleType) {
respond roleType
}

@Transactional
def update(RoleType roleType) {
if (roleType == null) {
transactionStatus.setRollbackOnly()
notFound()
return
}

if (roleType.hasErrors()) {
transactionStatus.setRollbackOnly()
respond roleType.errors, view:'edit'
return
}

roleType.save flush:true

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

@Transactional
def delete(RoleType roleType) {

if (roleType == null) {
transactionStatus.setRollbackOnly()
notFound()
return
}

roleType.delete flush:true

request.withFormat {
form multipartForm {
flash.message = message(code: 'default.deleted.message', args: [message(code: 'roleType.label', default: 'RoleType'), roleType.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: 'roleType.label', default: 'RoleType'), params.id])
redirect action: "index", method: "GET"
}
'*'{ render status: NOT_FOUND }
}
}
}

将代码添加到 Controller 后,我们得到:
URI
/roleType/save
Class
java.lang.RuntimeException
Message
null
Caused by
org.grails.datastore.mapping.validation.ValidationErrors: 0 errors

Around line 30 of grails-app\controllers\com\torntrading\legacy\RoleTypeController.groovy

27: @Transactional
28: def save(RoleType roleType) {
29: roleType.validate()
30: throw new RuntimeException("${roleType.errors}")
31: if (roleType == null) {
32: transactionStatus.setRollbackOnly()
33: notFound()

最佳答案

问题是您的 Controller 使用的是id,而您已将其替换为roleType

关于grails - 保存时发生Grails DB错误(没有域或没有标识符。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42885669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com