I have a method that includes transactionTemplate in it. My test works when I run debug mode but not when executed directly.
My test works when I run debug mode because I think transactional operation is done while working debug mode. But when I just run it works wrong. How can I overcome this problem?
This is my method:
@Override
public void saveAndUpdate(DataDictionaryDTO input) {
try {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
DataDictionaryEntity ent = null;
if (NullUtils.isNotNullOrZero(input.getId())) {
ent = dataDictionaryRepository.findById(input.getId()).orElse(null);
}
dataDictionaryRepository.save(dataDictionaryDTOMapper.map(input, ent));
}
});
}
catch (DataIntegrityViolationException e) {
throw new UserFriendlyException("there.is.already.such.a.value");
}
}