2017-03-19 20 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相关的映射。

相关问题