如何使用play Framework ebean将列表保存到数据库中?

时间:2015-11-21 12:42:27

标签: java database playframework ebean

我正在尝试通过子标题列表进行保存,但使用相同的部门ID但不同的subheadDepartmentID。我该如何去做?

subHeadDepartment.department= department;
    for(String thissubhead: ConstructedList){
         SubHead subHead = SubHead.retrievebyName(thissubhead);
         subHeadDepartment.subhead=subHead;
         subHeadDepartment.save();
        }

我这里的代码只是更新循环中的第一个subheadDepartment Id。虽然我想要的是为每个输入的子标题创建一个subheaddepartmentId,但所有子标题都在DB.Thanks中

1 个答案:

答案 0 :(得分:0)

您需要为要创建的每个元素创建一个新的SubHeadDepartment实例:

for(String thissubhead: ConstructedList){
     SubHead subHead = SubHead.retrievebyName(thissubhead);
     SubHeadDepartment subHeadDepartment = new SubHeadDepartment();
     subHeadDepartment.department = department;
     subHeadDepartment.subhead = subHead;
     subHeadDepartment.save();
    }