I have a small issue with deleting from a table which has several child entities.
I have a table with the name UNIVERSITY and one with the name STUDENT.
@OneToMany(mappedBy = "university", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Student> studentList = new ArrayList<>();
@ManyToOne(fetch = FetchType.EAGER)
private University university;
I whish to delete a row from the table University:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void deleteFromUniveristy(Long id) {
em.createNamedQuery(University.DELETE_BY_ID)
.setParameter("id", id)
.executeUpdate();
}
@NamedQuery(name = University.DELETE_BY_ID, query = "delete from University uu where uu.id =:id")
But a receive the error message:
java.sql.SQLIntegrityConstraintViolationException: ORA-02292: integrity constraint violated - child record found
I do not understand, the orphanRemoval = true and the given CascadeType does not garantee to be this deletion possible?
Thank you!
To delete parent with child is possible using entityManager.remove(entity)
method. Cascading does not work in delete JPQL.