2017-03-19 17 views
0

我正在開發一個新的應用程序,需要讀取傳統數據庫中的現有數據庫表。要做到這一點,我也必須讓它在開發環境中工作。但是,當我嘗試創建它失敗,出現以下消息的新記錄: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() 
+0

所以這個問題顯然是在控制器中,你更喜歡保持它的祕密實現? –

+0

對不起,但正如我所提到的,控制器是由腳手架(生成全部)創建的,所以它不是祕密,但我也可以發佈它。 – larand

+0

Ofcource將有助於發佈有問題的代碼。 –

回答

1

問題是您的控制器正在使用id,您將其替換爲roleType

+0

你是多麼懷特,發電機,當我更接近控制器,id爲roleType.id時,我發現自己。我沒有想到看到控制器,因爲它是由grails生成的,我認爲它可以修復所有內容,但顯然不是。並感謝你,安東,你把我的眼睛放在控制器上。 – larand

1

好吧,我不知道,但似乎角色類型有錯誤,但角色類型。 hasErrors()validate()save()之前被調用。

我想,如果你添加一些行到頂端:

def save(RoleType roleType) { 
    roleType.validate() 
    throw new RuntimeException("${roleType.errors}") 

    if (roleType == null) { 
     ... 
    } 
} 

你會看到領域和失敗的約束。

修訂

看起來很奇怪。我建議嘗試按照建議顯式重定向,或者簡化域中與id相關的映射。

相關問題