Cascaded merge fails for detached bytecode-enhanced entity with uninitialized ToOne

Description

Recently I enabled bytecode enhancement in an application using hibernate 5.2.17.Final (via maven plugin) .
The only option I use is <enableLazyInitialization>true</enableLazyInitialization> .
After that I noticed an unexpected LazyInitializationException in code that was working fine before the change .
Based on https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/chapters/pc/BytecodeEnhancement.html I added @LazyToOne(LazyToOneOption.NO_PROXY) on one ManyToOne relationship
This made lazy loading work but now if some code loads the entity without initializing this relationship, passes entity ouside of session scope and then tries to merge it in another session then it ends up with :
org.hibernate.LazyInitializationException: Unable to perform requested lazy initialization [classname] - no session and settings disallow loading outside the Session
Which is a bit weird because the same code without @LazyToOne and bytecode instrumentation returns both entities .
Is this issue known/documented ?

This lazy load exception is triggered by CascadeType.MERGE set in @ManyToOne annotation .

I reproduced the issue with code fetched from latest master and will submit a pull request in a moment .

Environment

None

Status

Assignee

Gail Badner

Reporter

Bolek Ziobrowski

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.4.0.CR2

Priority

Major