gpt4 book ai didi

hibernate - grails withSession 和当前的 hibernate session

转载 作者:行者123 更新时间:2023-12-03 06:47:27 29 4
gpt4 key购买 nike

我对 Grails 中的 withSession 和当前的 hibernate session 感到困惑。

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

我编写了一个服务,其操作如下:

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 个 session 对象,我会看到相同的 toString() 结果。这让我猜测它们具有相同的内容:

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

最佳答案

简单回答您的问题:
我们在闭包中访问的 session 对象不是当前的 hibernate session 。

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

看看source of withSession ,清楚地看到 HibernateTemplate 中的 setExposeNativeSession 设置为 false (默认值也是 false)这确保始终返回 session 代理而不暴露 native hibernate session 。

关于hibernate - grails withSession 和当前的 hibernate session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16710618/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com