I am using Spring JpaRepository for CRUD implementation in one of my App screens. As a part of that, I am developing a feature where user can save all Car entities or nothing (should roll back) into database. I am not sure how do I achieve this functionality using Spring Boot, Spring Rest, Spring Jpa.
Below is my source code.
@Repository
interface CarRepository extends JpaRepository<Car, Integer> { }
@Service
class CarService {
@Autowired
CarRepository repo;
@Transactional(rollbackFor=RuntimeException.class)
public List<Car> saveAllOrNone(List<Car> cars) {
for(Car car: cars) {
repo.save(car);
}
}
}
I am unable to figure out what wrong I am doing.
When I tested it using two different data among which 1 have invalid data, the other record is getting inserted into database instead of being rolled back.
In addition to this, I am getting an Exception like
UOWManager transaction processing failed:nested exception is com.ibm.wsspi.uow.UOWException: javax.transaction.RollbackException
Please help me out. Thank you.