gpt4 book ai didi

java - getCurrentSession() 与 openSession()

转载 作者:行者123 更新时间:2023-11-29 09:02:09 29 4
gpt4 key购买 nike

我正在尝试使用 generic-dao ( http://code.google.com/p/hibernate-generic-dao/ )。但是,在我的 HibernateBaseDAO 中,getSession() 方法是作为 sessionFactory.getCurrentSession() 实现的。这会导致任何实体更新出错

org.hibernate.HibernateException: createCriteria is not valid without active transaction

但是,当我使用 openSession() 代替 getCurrentSession() 时,它起作用了。我没有将 spring 用作 pom.xml 中的依赖项。我一直在阅读 openSession() 和 getCurrentSession(),但仍然不明白为什么会这样?

最佳答案

找到解决方案,使用 genericDAO 获取需要使用 openSession() 显式打开的当前 session ,而 getCurrentSession() 只是将其附加到 current session .据作者介绍

GenericDAO makes the assumption that you will be handling transactions externally to the DAO

关于java - getCurrentSession() 与 openSession(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16691950/

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