With the following entities, orphan removal works as expeted when setting parent.setChild(null). However it fails with an "EntityExistsException" when I try and set the child to a new Child object (parent.setChild(new Child());
Possibly related to HHH-11144.
Not related to HHH-11144.
I believe this is expected. After all, the new entity will ultimately have the same ID as the old (the same as the Parent#id). Hibernate assumes that 2 entities with the same ID represent the same entity. There can only be one representation of the same entity in the PersistenceContext.
I'm not sure what the intended behavior is. I'm fairly certain this works in eclipse link, and the jpa spec wasn't too clear about it.
If "Hibernate assumes that 2 entities with the same ID represent the same entity", then Hibernate is making assumptions that contradict the specification. Specifically, when the Java reference on a @MapsId is updated (particularly, set to null), the physical ID field is supposed to track it.
Please see also and HHH-9663.