Hi what i trying to achieve is to do "UPDATE" action and then do "DELETE" action with Spring Data JPA and @Transactional annotation, and i want both action is executed but if one of the action is failed, i need a rollback, how do i do this properly?
first here is my service class :
@Transactional
@Service
public class TransDeliveryPlanningService {
public ResponseRequest<TransDeliveryPlanning> deleteTransDeliveryPlanning(InputDeleteRequest<ViewAndDeleteRequest> request) {
String currentUser = request.getLoggedInUser();
String reasonDeleted = request.getReason();
Long id = request.getObject().getId();
ResponseRequest<TransDeliveryPlanning> response = new ResponseRequest<TransDeliveryPlanning>();
TransDeliveryPlanning transDeliveryPlanningOld = transDeliveryPlanningRepository.findById(id).orElseThrow(() -> new ResourceNotFound("Data "+ id +" not found!"));
transDeliveryPlanningOld.setIsDeleted(true);
transDeliveryPlanningOld.setReasonDeleted(reasonDeleted);
transDeliveryPlanningOld.setModifiedBy(currentUser);
TransDeliveryPlanning updatedTransDeliveryPlanning = transDeliveryPlanningRepository.save(transDeliveryPlanningOld);
transDeliveryPlanningRepository.delete(transDeliveryPlanningOld);
//NOTE delete transStuffing
List<TransStuffing> transStuffing = transStuffingRepository.findBydeliveryPlanningId(transDeliveryPlanningOld.getId());
Boolean deletePermit = false;
for(TransStuffing ts : transStuffing) {
if(ts.getStatus().equalsIgnoreCase("new")) {
deletePermit = true;
} else {
throw new ResourceIsDelete("Stuffing " + ts.getStuffingNo() + " Status already changed, delete is not permited!");
}
}
if(deletePermit){
transStuffingRepository.deleteAll(transStuffing);
}
//NOTE end of delete transStuffing
if(updatedTransDeliveryPlanning != null) {
response.setResponse("Sukses Hapus");
response.setObject(updatedTransDeliveryPlanning);
} else {
response.setResponse("Gagal Hapus");
}
return response;
}
}
as you can see, i do transDeliveryPlanningRepository.save
and then the next line i do transDeliveryPlanningRepository.delete
and the next repo i need to execute is transStuffingRepository.deleteAll
The goal i need to do save before delete is i use Hibernate Audit Envers to create an AuditLog, so i want log the delete reason into my audit table and then delete the record. But when i use **@Transactional**
annotation the transDeliveryPlanningRepository.save
(update) not executed, my function just execute transDeliveryPlanningRepository.delete
and transStuffingRepository.deleteAll
how i keep the save executed with @Transactional annotation?
UPDATE
As Morteza Bandi answer below suggestion, here is my updated code : here is my repository :
@Repository
public interface TransDeliveryPlanningRepository extends RevisionRepository<TransDeliveryPlanning, Long, Integer>, JpaRepository<TransDeliveryPlanning, Long> {
@Modifying
TransDeliveryPlanning save(TransDeliveryPlanning transDeliveryPlanning);
}
when i do this, still it's not update before delete, what did i still miss here?