Bytecode enhanced entities in OneToMany relationship with custom equals() implementation in child entity throws LazyInitializationException

Description

Bytecode enhanced entities in a OneToMany relationship throws

org.hibernate.LazyInitializationException: Unable to perform requested lazy initialization [...] - no session and settings disallow loading outside the Session

when accessing the OneToMany collection when the child entity implements equals/hashcode that refers to the lazily loaded parent.

A test case will be attached. The test case works if the entities are not bytecode enhanced.

When lazily loading the child entities of the OneToMany relationship, the equals method on the child entity is called as when adding the children to the OneToMany set on the parent. The equals implementation refers the the parent which is lazily loaded as well. The problem though is that the child entity is not fully initialized and has not had the session linked into its LazyAttributeLoadingInterceptor - it's still null (see PojoEntityInstantiator.applyInterception). Therefore, the lazy load of the parent cannot be performed as there is no session and a LazyInitializationException is thrown from Helper.performWork.

Environment

None

Status

Assignee

Unassigned

Reporter

Jay Paulsen

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure