How to save through a list into the Database, using play Framework ebean?

北城以北 提交于 2019-12-13 07:45:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!