1

I am new to spring. I am creating Rest API using spring boot with JPA. I want to add multiple rows to database MYSQL.

I have created controller where i am using save() method from repository to insert data into database. I want to add multiple rows to database but when i run the code only last value is added in database. If i try to create Userskill object for method each time it works fine but it is not feasible to create new object each time. This is portion of code of controller. Here, Userskill is model and table into which i want to insert a data.

    for(int i=0;i<listofskill.size();i++)
     {                           
         userskill.setUser_id(userid);           
         userskill.setSkill_id(skillRepo.findByName(listofskill.get(i)).getSkill_id());
         userskillRepo.save(userskill);          
     }                  

this code only add 1 row with last value. I want to add each value to the database.

4
  • You need to create a new object. The object represents the row in the db, as soon as it is saved, it is managed by JPA and all set methods will update the managed entity. Each row needs its own UsersSkill instance. Commented Aug 6, 2019 at 13:41
  • But what if i need to add 50 rows? It's not feasible to create 50 Userskill object. Commented Aug 6, 2019 at 14:05
  • Is there any other way? Commented Aug 6, 2019 at 14:05
  • As stated just create the objects. Why isn't it feasible, people all around the world,d are doing it like that. You are using JPA which means you have to work in the JPA way... If you don't want that don't use JPA. Commented Aug 6, 2019 at 14:08

2 Answers 2

2

You can create list of Userskill then add all the elements at once, check below code:

List<UserSkillObject> userskillList = new ArrayList<>();

for(int i=0;i<listofskill.size();i++){
   UserSkillObject userskill = new UserSkillObject();
   userskill.setUser_id(userid);           
   userskill.setSkill_id(skillRepo.findByName(listofskill.get(i)).getSkill_id());
   userskillList.add(userskill);
}
userskillRepo.saveAll(userskillList);
Sign up to request clarification or add additional context in comments.

Comments

1

try this

for(int i=0;i<listofskill.size();i++)
     {     
         UserSkillObject userskill  = new UserSkillObject ();
         userskill.setUserSkillId(newId);           
         userskill.setUser_id(userid);           
         userskill.setSkill_id(skillRepo.findByName(listofskill.get(i)).getSkill_id());
         userskillRepo.save(userskill);          
     }

2 Comments

This solves the problem but you're making multiple opens to the database. There should be one tripe to add all of the data.
@eyoeldefare you can add all the in the list at once using saveAll API, but for that you need to create list of Objects first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.