1

I want to save multiple objects at once and for that I am using CrudRepository's saveAll method in spring boot. But object have one property with unique constraint in mysql. So when I tried to saveAll method it is showing me duplication entry exception i.e. org.springframework.dao.DataIntegrityViolationException. I want to save all others objects which doesn't get exception of duplicate entry. Can anyone having this type of requirement? Please let me know if you have any solution.

1 Answers1

1

Instead of using saveAll method, use below

for(int i=0; i<list.size(); i++) {
            try {
               save(list.get(i));
            } catch(DataIntegrityViolationException e){
            continue;
         }
      }
vinay jain
  • 93
  • 6
  • Is it possible to catch exception with saveAll method? Actually, I have large number of records which I want to save at once. Single record save at once will take more time right? – Raman Joshi Sep 10 '21 at 05:10
  • https://stackoverflow.com/questions/64754720/hibernate-transactions-and-concurrency-using-attachdirty-saveorupdate/64764412#64764412 - Check this out, It might resolve your problem.. – vinay jain Sep 10 '21 at 16:46