In my Java EE application, I have two persistent entities Worklist
and Task
with these JPA relationships:
Worklist
entity:
public class WorkList implements Serializable {
...
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.REMOVE} , mappedBy ="worklist")
private List<Task> tasks;
...
}
And the Task
entity:
public class Task implements Serializable {
...
@ManyToOne
private WorkList worklist;
...
}
I indicated the cascade type in Worklist
as:
cascade={CascadeType.PERSIST, CascadeType.REMOVE}
So, I can persist a Worklist
and a list of tasks
that are associated to this Worklist
but I have a problem when I try to delete a Worklist
and I have this exception:
Avertissement: Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot
delete or update a parent row: a foreign key constraint fails (`scopedata`.`TASKS`,
CONSTRAINT `FK_TASKS_WORKLIST_id` FOREIGN KEY (`WORKLIST_id`) REFERENCES `WORKLISTS`
(`id`))
Error Code: 1451
Call: DELETE FROM WORKLISTS WHERE ((id = ?) AND (version = ?))
bind => [2 parameters bound]
Query: DeleteObjectQuery(WokList{id=bbf05bdb-1314-4b07-9f4e-69898b800c00, creation=Thu
Nov 28 08:39:21 CET 2013, startDate=Thu Nov 07 00:00:00 CET 2013, endDate=Fri Nov 15
00:00:00 CET
On the other side, I can successfully delete a Task
.
This is the methode in the WorklistManager.java that removes a worklist
@Override
public boolean deleteWorkList(WorkList workList) {
if (findWorkList(workList)) {
em.remove(em.merge(workList));
return true;
}
return false;
}
Then, I call this methode in the Managed Bean:
public String deleteWorkList() {
workListManagerLocal.deleteWorkList(selectedWorkList);
return "index";
}
I am working with: JPA2
and Glassfish 3.1.2.2
.