grails withSession和当前的hibernate会话(grails withSession and current hibernate session)

编程入门 行业动态 更新时间:2024-10-22 17:16:34
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 ......“

I am confused between withSession in Grails and current hibernate session.

My question is: Is the session object which we have access to in the closure the same as the current hibernate session object?

I wrote a Service that has an action as below:

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() } }

What is strange for me is that there are 5 different hash codes... If I print the 5 session objects, I see the same toString() result. That makes me guess that they have the same contents:

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

最满意答案

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

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

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

To answer your question briefly: The session object which we have access to in closure is not the current hibernate session.

The session object is a proxy of the current hibernate session. Hence different hash codes in each case.

Have a look at source of withSession, clearly seen setExposeNativeSession is set to false (default value is false as well) in HibernateTemplate which makes sure to always return a Session Proxy without exposing the native hibernate session.

更多推荐

本文发布于:2023-08-07 11:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:withSession   grails   hibernate   session   current

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!