【问题标题】:Getting org.hibernate.HibernateException: collection is not associated with any session获取 org.hibernate.HibernateException:集合未与任何会话关联
【发布时间】:2018-09-16 05:33:02
【问题描述】:

我在获取数据库中表中存在的所有条目时遇到异常。

我收到的错误是:

org.hibernate.HibernateException: collection is not associated with any session
        at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:678) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
        at org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:893) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:360) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
        at org.hibernate.loader.Loader.doList(Loader.java:2554) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
        at org.hibernate.loader.Loader.doList(Loader.java:2540) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]

令我震惊的是,我在测试期间没有收到此错误,但它在生产环境中出现,这让我感到困惑!

这里有人可以帮我弄清楚这种奇怪的行为吗??

和桌子的大小有关系吗??

【问题讨论】:

  • stackoverflow.com/questions/18956825/… 这可能会有所帮助。确保会话已初始化
  • 我已经想要这个答案并得到它。但是我想知道的是为什么我在本地测试时没有抛出这个异常!!
  • 请添加您的代码以了解有关该问题的更多信息。

标签: java spring postgresql hibernate jpa


【解决方案1】:

将@Transactional 注解放在方法之上。

【讨论】:

  • 谢谢,我也看到了其他人建议的这个解决方案。但我真正想知道的是我在测试环境和生产环境中的行为差异。基本上,我想知道为什么在我的本地测试期间没有抛出这个异常!
猜你喜欢
  • 1970-01-01
  • 2013-09-28
  • 2012-06-11
  • 1970-01-01
  • 2011-03-06
  • 2017-12-03
  • 1970-01-01
  • 2013-08-23
  • 1970-01-01
相关资源
最近更新 更多