Hibernate ManyToOne with FetchType.LAZY not fetching lazy

♀尐吖头ヾ 提交于 2019-11-30 18:35:20

I guess it's caused by the fact that your classes are declared as final, therefore Hibernate cannot generate lazy proxies for them. Try to remove final from class declarations.

Looks like the transaction/session is closed after returning ForumTopic. Hence it becomes detached entity. When you try to do a getForumCategory there is no session to do the operation. You can prefetch the ForumCategory in the same session like,

In your getForumTopic, before returning a list (assuming you have a getAllForumTopic)

public List<ForumTopic> getAllForumTopic() {
  <snip> 
  List<ForumTopic> topics = <SNIP: get the list of ForumTopic>

  for(ForumTopic topic: topics)
      topic.getForumCategory()

  return topics;
}

This will fetch the ForumCategory in the same session. Otherwise you have to create a transaction in the calling function of getAllForumTopic and use the same transaction where ever you need to call getForumCategory.

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