如何在域中导入服务?

时间:2012-10-08 16:28:06

标签: grails

如何在域中导入服务?

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

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

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

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

3 个答案:

答案 0 :(得分:4)

postService不应该是静态的,应该只是

transient postService

答案 1 :(得分:1)

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

AfterInsert def () {

     if (! this.protocolo) {

             Post.withNewSession
             {

                  registraProtocolo ()


             }

     }

 }

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

谢谢大家的帮助

For those who want more information down a JIRA who helped me in this solution (read the comments)

答案 2 :(得分:0)