0

I'm trying to save a list of entity objects. Here is my method which is defined in a Service class:

@Transactional
public void creteEmployee(List<Employee> employeeList) {
    employeeRepository.saveAll(employeeList);
}

And here is my JPA repository:

public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {
    Page<Employee> findByCompany(Pageable pageable, Company company);
}

I've a unique constraint defined in my entity. If any item from the employeeList violates the unique constraint, I expect no one from the employeeList will be saved. Unfortunately all items except the problematic one is saved and also an DataIntegrityViolationException is raised. What should be done to make it atomic?

Nayan
  • 1,521
  • 2
  • 13
  • 27

0 Answers0