Hibernate cascade remove ConstraintViolationException

Viewed 2788

It seems the situation is simply (but it does not work). Db part (EVENT_ID is foreign key. FK_RR_E_CI constraint references on EVENT table)

 |-------|            |----------------|
 | EVENT | 1 ------ ∞ | RECURRENT_RULE |
 |-------|            |----------------|
 | ID    |            | ID             |
 |-------|            | EVENT_ID       |
                      |----------------|

Java part:

@Entity
public class Event {
  @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "event")
  private Set<RecurrentRule> recurrentRules = new HashSet<>();
}

@Entity
public class RecurrentRule {
  @ManyToOne
  @JoinColumn(columnDefinition = "event_id")
  private Event event;
}

If I try to delete event object It will return:

could not execute statement; SQL [n/a]; constraint [MY_SCHEMA.FK_RR_E_CI]; 
nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
...
java.sql.SQLIntegrityConstraintViolationException: ORA-02292: integrity constraint (MY_SCHEMA.FK_RR_E_CI) violated - child record found

SAVE and UPDATE operations work correctly.


What should I change in my mapping to be able use cascade removal? I know that I should use @OnDelete(action=OnDeleteAction.CASCADE) but I can't understand how to use it...

2 Answers
Related