1

如何在域中导入服务?

它有一个字段,我需要用协议填写一个字段。该协议是自动生成的,并为这一代创建了一个服务excluvivamente。

在方法“AfterInsert”的字段中插入了对该服务的调用,该服务会自动填充该字段。

我启动了一些对象的创建,这些对象需要使用此协议填充到您的字段中。但是发生了一个错误,这显然是由于在“域”中使用了“服务”。有人可以帮我吗?

class Post {

    static transient postService

    String conteudo
    Date dataCriacao = new Date()
    String protocolo

    static constraints = {

        dataCriacao(nullable:false, blank:false)
        conteudo nullable:false, blank: false 
        protocolo nullable: true, blank: true 

    }

    static mapping = {  
        conteudo type: 'text'
        sort dataCriacao:"desc"   
    }

    def afterInsert(){
        if(!this.protocolo){                   
            registraProtocolo()
        }
    }

    protected void registraProtocolo() {
       postService.teste(this)
    }
}

Error: ERROR hibernate.AssertionFailure  - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in com.app.post.Post entry (don't flush the Session after an exception occurs)
    Line | Method
->>  105 | doCall             in org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2

Message: null id in com.app.post.Post entry (don't flush the Session after an exception occurs)
    Line | Method
->>  105 | doCall             in org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     27 | recInsertProtocolo in com.app.post.PostService
|     83 | teste . . . . . .  in     ''
|    117 | registraProtocolo  in com.app.post.Post
4

3 回答 3

4

不应该是静态的postService,应该是简单的

transient postService
于 2012-10-08T17:49:19.087 回答
1

问题解决了!这是一个逻辑问题。该服务自动设置为“transational = true”,使用 AfterInsert 错误是由于该服务的此功能而发生的。但是如果你使用闭包'withNewSession',这个问题就解决了,一旦新会话满足'transational'的要求,就可以改变对象的属性。刚得到我的域是这样的:

AfterInsert def () {

     if (! this.protocolo) {

             Post.withNewSession
             {

                  registraProtocolo ()


             }

     }

 }

 protected void registraProtocolo () {
    postService.teste (this)
 }

谢谢大家的帮助

对于那些想要在这个解决方案中帮助我的 JIRA 获得更多信息的人(阅读评论)

于 2012-10-09T12:40:17.790 回答
0
class Post {

    def postService

    ...
} 

参考: http: //grails.org/doc/2.1.0/guide/single.html#dependencyInjectionServices

于 2012-10-09T04:45:49.507 回答