问题
I'm trying to save through a list of subheads but with the same Department Id but different subheadDepartmentID. How do i Go about it?
subHeadDepartment.department= department;
for(String thissubhead: ConstructedList){
SubHead subHead = SubHead.retrievebyName(thissubhead);
subHeadDepartment.subhead=subHead;
subHeadDepartment.save();
}
The code I have here is updating just the first subheadDepartment Id in the loop.While what i want is to create a subheaddepartmentId for each subhead entered but all will have same departmentId in the DB.Thanks
回答1:
You need to create a new instance of SubHeadDepartment for each element you want to create:
for(String thissubhead: ConstructedList){
SubHead subHead = SubHead.retrievebyName(thissubhead);
SubHeadDepartment subHeadDepartment = new SubHeadDepartment();
subHeadDepartment.department = department;
subHeadDepartment.subhead = subHead;
subHeadDepartment.save();
}
来源:https://stackoverflow.com/questions/33843484/how-to-save-through-a-list-into-the-database-using-play-framework-ebean