Hibernate LazyInitializationException on find() with EAGER @ElementCollection

怎甘沉沦 提交于 2019-12-06 08:23:42

First of all it is useful to reference the full stack trace: http://pastie.org/4358203

The problem is being caused by your call to tags.hashCode() within the hashCode() implementation of MyEntity.

When you use Hibernate to load the MyOtherEntity instance, it's collection of MyEntity is initialised. When a MyEntity is added to the Set implementation within MyOtherEntity, it's hashCode() method is naturally called (sets cannot contain duplicates remember and hashCode() is part of how Java checks for object equality). The hashCode() method of MyEntity then attempts to invoke hashCode() on the tags collection. The tags collection is in the process of being initialised, which leads you to this error.

I think it is worth thinking about your hashCode() implementation for MyEntity. Does your use-case really require you to compare the value of the all elements within the tags collection to ensure object equality?

For more information on managing object equality in Hibernate, the following is a useful resource:

https://community.jboss.org/wiki/EqualsAndHashCode

LazyInitializationException – Indicates access to unfetched data outside of a session context. For example, when an uninitialized proxy or collection is accessed after the session was closed.

Some things you might want to try:

Removed @Cleanup - As a LazyInitializationException usually means that the Hibernate session was closed when a proxy tried to access a field, you should try it without those @Cleanup annotations. I never used them myself, but the docs say that the variable declaration gets cleaned up by calling .close() "the end of your scope".

Doublecheck configuration - Still odd, as you declare FetchType.EAGER for both associations. Have you checked that these options really get used? I know that the configuration might sometimes get a little tricky.

PersistenceContextType - You might want to try to set PersistenceContextType.EXTENDED for your EntityManager (I think TRANSACTION is the default, could be wrong but you might want to try to be sure).

In the case of a transaction-scoped persistence context, the entities become detached, that is, they are no longer managed. In the case of an extended persistence context, the entities remain managed.

I guess you already know the "LazyInitializationException overcome" wiki entry?

The problem is that Hibernate ignores fetch = FetchType.EAGER for most queries. Try adding @Fetch(FetchMode.JOIN) to entities.

See: https://community.jboss.org/wiki/HibernateFAQ-AdvancedProblems#Hibernate_ignores_my_outerjointrue_or_fetchjoin_setting_and_fetches_an_association_lazily_using_n1_selects

This error Appears when hibernate try to initialize object and some error occur.

Error Can be

  1. Hash Code And Equals Not properly implemented
  2. the lazy object is expected to be encrypted but in db it is not properly encrypted
  3. you are outside the hibernate session context.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!