我正在尝试使用 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/