When using Hibernate Session to remove a ManyToOne Child Entity, EntityNotFoundException will raise up.
jakarta.persistence.EntityNotFoundException: deleted object would be re-saved by cascade (remove deleted object from associations): [org.hibernate.bugs.entities.Entity2#2_1 ]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:175)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:182)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1426)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1408)
at org.hibernate.bugs.ORMUnitTestCase.removeEntity2_1(ORMUnitTestCase.java:78)
If the EntityManager is used instead, the exception will not be thrown. Therefore the Session.remove() call has no effect because the entity is saved again via cascading. ORM Testcase sample based on hibernate-test-case-template can be found here: https://github.com/PeRaVs/hibernate-sample Look at ORMUnitTestCase.java (based on Session class, with EntityNotFoundException raised up) Look at JPAUnitTestCase.java (based on EntityManager, without EntityNotFoundException) |