我正在開發一個新的應用程序,需要讀取傳統數據庫中的現有數據庫表。要做到這一點,我也必須讓它在開發環境中工作。但是,當我嘗試創建它失敗,出現以下消息的新記錄:Grails數據庫在保存時發生錯誤(不是域或者沒有標識符)
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
爲了讓我跑了「Grails的生成所有」的控制器和視圖。
域名,在這裏我去掉了清晰一些領域,像這樣:
class RoleType {
int roleType
static mapping = {
table 'RoleType'
version false
id name: 'roleType', type:'int', generator:'assigned'
roleType column: 'RoleType'
}
}
我不知道他們的意思有:「不是域或沒有標識」和他們是什麼意思明確的重定向,我應該重定向到什麼?這是唯一的解決方案 - 我不能相信這一點。
控制器:
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 }
}
}
}
將代碼添加到我們得到的控制器之後:
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()
所以這個問題顯然是在控制器中,你更喜歡保持它的祕密實現? –
對不起,但正如我所提到的,控制器是由腳手架(生成全部)創建的,所以它不是祕密,但我也可以發佈它。 – larand
Ofcource將有助於發佈有問題的代碼。 –