In Play Framework! I defined two models :
On one hand,
@Entity
public class DashboardPosition extends Model {
public int orderId;
@ManyToOne(cascade=CascadeType.ALL)
public Dashboard dashboard;
}
And on the other hand,
@Entity
public class Dashboard extends Model {
...
}
For some reason, when deleting a dashboard via “dashboard.delete()” I get an error with the following log.
12:07:20,190 DEBUG ~ delete from Dashboard where id=?
12:07:20,204 WARN ~ SQL Error: 23003, SQLState: 23003
12:07:20,204 ERROR ~ Referential integrity constraint violation: "FKF7C253BD5A49DA96: PUBLIC.DASHBOARDPOSITION FOREIGN KEY(DASHBOARD_ID) REFERENCES PUBLIC.DASHBOARD(ID)"; SQL statement:
delete from Dashboard where id=? [23003-149]
12:07:20,204 WARN ~ SQL Error: 23003, SQLState: 23003
12:07:20,204 ERROR ~ Referential integrity constraint violation: "FKF7C253BD5A49DA96: PUBLIC.DASHBOARDPOSITION FOREIGN KEY(DASHBOARD_ID) REFERENCES PUBLIC.DASHBOARD(ID)"; SQL statement:
delete from Dashboard where id=? [23003-149]
Which makes me think that cascade is not working in my case.
Any clue of why this might be happening / how to debug this problem?
The cascade is on the wrong side of the association.
You want all the dashboard’s positions deleted when you delete a dashboard. But you don’t want the dashboard deleted when you delete one of its positions. The cascade should thus be set on the
@OneToManyannotation of the Dashboard’spositionsfield.