Beanieman
Beanieman

Reputation: 7

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

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

Upvotes: 0

Views: 308

Answers (1)

Gus
Gus

Reputation: 4517

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();
    }

Upvotes: 0

Related Questions