0

This seems to be a popular topic regarding hibernate, both on stackoverflow (IllegalStateException with Hibernate 4 and ManyToOne cascading) and outside (https://forum.hibernate.org/viewtopic.php?f=1&t=1031016&view=previous) but it is still unclear to me.

I have object A, I merge it with entity manager E1. I close E1. I have object B, it references the instance of A we merged with E1. B has cascaded merge & persist on A.

I merge B with entitymanager E2 and i get the exception "Error occurred while storing entity [...]. An entity copy [...] was already assigned to a different entity [...]."

All my searching seems to indicate they introduced some additional check somewhere in hibernate version 4.1.3 or there-abouts. In fact, if I run the exact same code with 4.1.2, it works.

The additional check supposedly entails a proper equals() and hashCode() implementation, but my objects have those and I have added debug logging to them: they are never called.

I can "solve" it by removing the cascade feature but...well...it's handy...

How do I solve this issue so cascading can work as it is supposed to?

UPDATE

Apparently the issue currently is that B actually references A twice. So I assume the second detached A is throwing the exception. I can see how this could be that B is merged, the first instance of A is attached and the second throws an exception.

However is there no way to force hibernate to simply use the reattached first version of A for the second A? It will be very hard to guarantee that A is reattached only once in my context.

Community
  • 1
  • 1
nablex
  • 4,635
  • 4
  • 36
  • 51

1 Answers1

0

It seems to be a known issue: https://hibernate.atlassian.net/browse/HHH-9106

There is no feedback as to "why" this is not fixed.

I have tried rolling back the EventCache (which is the one throwing the exception) to the 4.1.2 version which contains far less checking. I have tried fiddling with the DefaultMergeEventListener to no avail. I have no time to dig deeper so I have had to remove all the cascading from my @ManyToOne relationships to prevent this issue from popping up.

nablex
  • 4,635
  • 4
  • 36
  • 51