Gre*_*ydp 4 session grails hibernate
我对 Grails 中的 withSession 和当前的休眠会话感到困惑。
我的问题是:我们在闭包中访问的会话对象是否与当前的休眠会话对象相同?
我编写了一个具有如下操作的服务:
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()
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说奇怪的是,有 5 个不同的哈希码...如果我打印 5 个会话对象,我会看到相同的 toString() 结果。这让我猜测它们具有相同的内容:
SessionImpl(PersistenceContext[entityKeys=[EntityKey[com.astek.agileFactory.Link#170], EntityKey[com.astek.agileFactory.Project#9]],collectionKeys=[Coll......"
简单回答你的问题:
我们在闭包中访问的会话对象不是当前的休眠会话。
会话对象是当前休眠会话的代理。因此每种情况下都有不同的哈希码。
看一下withSession 的源代码,可以清楚地看到HibernateTemplatesetExposeNativeSession中的设置为false(默认值也是 false),这确保始终返回会话代理而不暴露本机休眠会话。
| 归档时间: |
|
| 查看次数: |
2483 次 |
| 最近记录: |