Accessing a lazy collection in an enhanced class deletes it afterwards

Description

I'm trying to migrate from 4.3 to 5.1 using the new bytecode enhancer.

Given a class Foo with a collection of Bars with orphanRemoval=true, even just accessing the collection results in an exception:

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: org.hibernate.test.bytecodeissue.Foo.bar

See attached test case.

The second test case shows the issue without orphanRemoval in an enhanced class: Bar has a @ManyToMany annotated Set of Foos that are lazy loaded. The test case shows the following output:

1 2 3 4 5 6 7 8 9 10 11 12 (...) Hibernate: /* insert org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Bar2 */ insert into Bar2 (id) values (?) Hibernate: /* insert org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Foo2 */ insert into Foo2 (id) values (?) Hibernate: /* insert org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Foo2 */ insert into Foo2 (id) values (?) Hibernate: /* insert collection row org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Bar2.foos */ insert into Bar2_Foo2 (Bar2_id, foos_id) values (?, ?) Hibernate: /* insert collection row org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Bar2.foos */ insert into Bar2_Foo2 (Bar2_id, foos_id) values (?, ?) Hibernate: /* load org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Bar2 */ select bar2x0_.id as id1_0_0_ from Bar2 bar2x0_ where bar2x0_.id=? Hibernate: select foos0_.Bar2_id as Bar1_1_0_, foos0_.foos_id as foos_id2_1_0_, foo2x1_.id as id1_2_1_ from Bar2_Foo2 foos0_ inner join Foo2 foo2x1_ on foos0_.foos_id=foo2x1_.id where foos0_.Bar2_id=? 2016-04-26 14:57:05,875 ERROR [Time-limited test] (TestCase2.java:81) collection = [org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Foo2@7de4e70d, org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Foo2@5ed545b9] Hibernate: /* delete collection org.hibernate.test.hhh10708_bytecodeissue_wo_orphanremoval.Bar2.foos */ delete from Bar2_Foo2 where Bar2_id=? 2016-04-26 14:57:05,882 ERROR [Time-limited test] (TestCase2.java:83) DONE! (...)

The collection is deleted after it was accessed in the log error statement.

The cases works fine in test case when I set doDirtyCheckingInline to false in the EnhancementTask - but in our live environment with far more complex classes, I can achieve the same behaviour with disabled doDirtyCheckingInline.

Everything works fine without bytecode enhancement.

Environment

Hibernate 5.1.0

Status

Assignee

Steve Ebersole

Reporter

Niko Wittenbeck

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Priority

Critical
Configure