hibernate一對多,更新主表時刪除舊的多方數據加入新的多方數據

	@OneToMany(fetch = FetchType.EAGER)
	@Cascade(value = {CascadeType.SAVE_UPDATE,CascadeType.ALL}) 
	private List<Student> studentList; //集合列表


在service層中。更新主表前,先獲取舊的集合子對象,然後在更新完主表後,對舊的集合對象進行刪除


Classroom classroom=classroomDao.findById(id);
List<student> tempList=classroom.getStudentList();
classroom.setStudnetList(myNewStudnetList);
				
if(tempList!=null)
	for(Student item: tempList)
	{
		studentDao.remove(item);
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章