Hibernate collection is not associated with any session

房东的猫 提交于 2019-11-29 01:21:12
Roberto Rodriguez

I fixed this issue just putting @Transactional at top of the method.

To prevent a LazyLoadingException in my application, I use a helper method that first merges the entity back to the session and then initializes the requested collections. It recently turned out, that sometimes it still suffers from the "collection is not associated with any session" issue despite the session seems to be open and looks fine overall. It's hard for me to say what is the cause of such behavior, but still I found a remedy against it. Try to refresh the entity state before fetching the lazy collection using session.refresh(entity). This will cause Hibernate to reload your entity from the DB. After doing this, your current session will let you to load your collections smoothly with Hibernate.initialize().

As @Zmicier Zaleznicenka has pointed out, this exception message may have nothing to do with the Session. We have also encountered this problem, but the root cause was the entity primary key setting defined in the mapping file was incorrect. For example, in the following code, the database column "Foo_SK" is not the unique identifier of the table.

Id(x => x.Id, "Foo_SK").GeneratedBy.Assigned();

In our case, we have no control over the database schema. Therefore, we fixed the problem by using composite key mapping.

Please check the other post for the same issue.

I too had this problem however the problem for me was that i wasnt configuring the transactional management in my config file so :

add @EnableTransactionManagement to java Config file or add <tx:annotation-driven/> to xml config file

Nitheesh Chandran

I fixed this issue just putting @Transactional at top of the method. And also get the class by its id like this:

Test s=get(test.getId())
Angelo Fuchs

It seems like you call this in your init method of AbstractIntegrationManagedBean.

You have to make sure that hibernate gets set up properly before that point.

Most likely your configuration does not reflect that and you assume hibernate to be already there, although there is no guarantee for that.

See this question for more details: How to control order of bean init-method invocation in Spring?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!