繁体   English   中英

grails withSession和当前的hibernate会话

[英]grails withSession and current hibernate session

我在Grails中的withSession和当前的hibernate会话之间感到困惑。

我的问题是:我们在闭包中访问的会话对象是否与当前的hibernate会话对象相同?

我写了一个服务 ,其行为如下:

def strangeBehavior(){

    Link.withSession { session->
        println "link current session " +  session.hashCode()
    }

    Task.withSession { session->
        println "task current session " +  session.hashCode()
    }

    Project.withSession { session->
        println "project current session " +  session.hashCode()
    }

    UserStory.withSession { session->
        println "user story current session " +  session.hashCode()
    }

    def ctx = AH.application.mainContext
        def sessionFactory = ctx.sessionFactory
        def tmp = sessionFactory.currentSession
        println " current session " +  tmp.hashCode()
    }
}

对我来说奇怪的是有5个不同的哈希码...如果我打印5个会话对象,我会看到相同的toString()结果。 这让我猜他们有相同的内容:

SessionImpl(PersistenceContext [entityKeys = [EntityKey [com.astek.agileFactory.Link#170],EntityKey [com.astek.agileFactory.Project#9]],collectionKeys = [Coll ......“

简要回答你的问题:
我们在闭包中访问的会话对象不是当前的hibernate会话。

会话对象是当前hibernate会话的代理。 因此在每种情况下都有不同的哈希码

看一下withSession的来源 ,清楚地看到setExposeNativeSessionHibernateTemplate中设置为false (默认值也是false),这确保始终返回会话代理而不暴露本机hibernate会话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM