在@OneToMany
双向中,当EntityManger
更新具有 的集合的 ProjectValue 时Task
,
EntityManger
不能删除Task
内存中程序删除的子项。会Task
用其他功能删除吗?
DataStructure :
In ProjectPlan Table :
PRPJ0001, First Poject
In Task Table :
AAA, AAA, PRPJ0001
BBB, BBB, PRPJ0001
CCC, CCC, PRPJ0001
In Memory :
PRPJ0001, First Poject
AAA, AAA, PRPJ0001 --> No Changes
BBB, Change Name, PRPJ0001 --> Need to update
--> Need to delete CCC
NEW, NEW, PRPJ0001 --> Need to add
Problem : Entity Manger cannot delete CCC;
@Entity
public class ProjectPlan {
@Id
private String id;
private String name;
@OneToMany(mappedBy = "projectPlan", targetEntity = Task.class, cascade = {CascadeType.ALL})
private List<Task> taskList;
public ProjectPlan(){}
public ProjectPlan(String id, String name, List<Task> taskList) {
this.id = id;
this.name = name;
this.taskList = taskList;
}
//getter & setter
}
@Entity
public class Task {
@Id
private String id;
private String name;
@ManyToOne(targetEntity = ProjectPlan.class, cascade = {CascadeType.REFRESH})
@JoinColumn(name = "projectId", referencedColumnName = "ID")
private ProjectPlan projectPlan;
public Task(){
}
public Task(String id, String name, ProjectPlan projPlan) {
this.id = id;
this.name = name;
this.projectPlan = projPlan;
}
// getter & setter
}
public class Test {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA");
EntityManager em = emf.createEntityManager();
/*
Existing Data in Database
List<Task> taskList = new ArrayList<Task>();
ProjectPlan plan = new ProjectPlan("PRPJ0001", "First Poject", taskList);
Task task1 = new Task("AAA","AAA", plan);
Task task2 = new Task("BBB","BBB", plan);
Task task3 = new Task("CCC","CCC", plan);
taskList.add(task1);
taskList.add(task2);
taskList.add(task3);
*/
em.getTransaction().begin();
List<Task> taskList = new ArrayList<Task>();
ProjectPlan plan = em.find(ProjectPlan.class, "PRPJ0001");
/*Task "AAA" no changes*/
Task task1 = new Task("AAA","AAA", plan);
/*Task "BBB" updated info*/
Task task2 = new Task("BBB","Change Name", plan);
/*Task "CCC" removed*/
/*Task "NEW" newly created*/
Task task3 = new Task("NEW","NEW", plan);
taskList.add(task1);
taskList.add(task2);
taskList.add(task3);
plan.setTaskList(taskList);
em.merge(plan);
em.getTransaction().commit();
em.close();
}
}
如果Task
有另一个Sub Task List instance
(递归关系),Task
也会面临这种情况。我必须检查并检索需要务实删除的任务?你认为JPA
应该支持这个功能吗?