Prerequisite:
- bidirectional @OneToOne relationship:
*@OneToOne( mappedBy = "slot", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
*@OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "id") @MapsId @OnDelete(action = OnDeleteAction.CASCADE)
- "on delete cascade" foreign key;
- JPA.
While deleting the owner of this relationship via entityManager.remove, it doesn't have any impact on an underlying database. Having said that, ORMUnitTestCase works fine, but it's not the case with JPAUnitTestCase. Please, take a look at both of then attached to this issue. The workaround is not to trigger lifecycle callbacks and use entityManager.createQuery("delete...).executeUpdate(); Here are 3 test cases: //1 it doesn't work, but it's supposed to entityManager.remove(entityManager.find(Slot.class, 1L)); //2 it works, but we are not supposed to delete Automobile manually. //This is because, it's done by database via "on delete cascade" FK entityManager.remove(entityManager.find(Automobile.class, 1L)); entityManager.remove(entityManager.find(Slot.class, 1L)); //3 it works, because no one lifecycle callbacks can be invoked entityManager.createQuery("delete from Slot s where s.id =:id") .setParameter("id", 1L) .executeUpdate(); |