I have three entities: Person, City and PersonCity. The PersonCity entity is a mapped M:N table between Peron and City which uses an @EmbeddedId as the primary key attribute. The other entities, Person and City, both use a @OneToMany as a reference back to PersonCity. The mapping of the M:N table is required to store additional information about an association.
First I create a City and a Person and connect them via a PersonCity entity. Then I start a new transaction, load the created City using a LoadGraph and detach it immediatelly. That detach causes a PersistentObjectException when the transaction gets committed.
The exception doesn't get thrown when I comment out the call to EntityManager.detach. Debugging into the Hibernate code reveals, that Hibernate tries to persist the City entity which is referenced by the previously created (and already persisted) PersonCity object. It cascades a persist operation through PersonCity.city even though there is no cascade specified on the @ManyToOne reference. Moreover, I'm no sure if that persist is really required in that situation.
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)
Hibernate Version: 5.2.10.Final
Database (Test): H2 1.4.196