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.
Asked
Active
Viewed 608 times
1
-
Filter before saving? – yoni Sep 09 '21 at 07:35
-
@yoni for filtering I have to check each record with each record present in database. For large number of records it will take time. – Raman Joshi Jan 26 '22 at 05:36
1 Answers
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