I am new for hibernate, how to use @Transactional in spring,
this is inf class,
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackForClassName = {
"com.framework.exceptions.ApplicationException" })
public ServiceObject create(ServiceObject dtObject) throws ApplicationException;
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackForClassName = {
"com.framework.exceptions.ApplicationException" })
public ServiceObject update(ServiceObject dtObject) throws ApplicationException;
Implementation class,
public ServiceObject read(ServiceObject dtObject) throws ApplicationException {
try {
if (dtObject.getDataObject() != null) {
CustomersEntity customersEntity = (CustomersEntity) dtObject.getDataObject();
String captchaStr = customersEntity.getCaptchaString();
if (captchaStr != null && !captchaStr.isEmpty()) {
customersEntity.setCaptchaType(captchaStr.split(",", 2)[0]);
customersEntity.setCaptcha(captchaStr.split(",", 2)[1].getBytes());
customersEntity.setCaptchaString(null);
customersEntity.setUpdatedOn(DateUtil.getISTTodayDate());
if(customersEntity.getResult() == null || customersEntity.getResult().isEmpty()){
customersEntity.setWip((byte) 0); // if user not submit the captcha value
}
dtObject.setDataObject(customersEntity);
update(dtObject);
CustomersEntity customersEntity2 = (CustomersEntity) dtObject.getDataObject();
customersEntity2.setUserId(customersEntity2.getSolvedBy());
customersEntity2.setUpdatedOn(DateUtil.getISTTodayDate());
dtObject.setDataObject(customersEntity2);
CustomersEntity customersEntity3 = getCaptcha(dtObject);
if (customersEntity3 != null) {
byte[] imgByte = customersEntity3.getCaptcha();
customersEntity3
.setCaptchaString(customersEntity3.getCaptchaType() + "," + new String(imgByte));
customersEntity3.setWip((byte) 1);
customersEntity3.setUpdatedOn(DateUtil.getISTTodayDate());
dtObject.setDataObject(customersEntity3);
update(dtObject);
} else {
dtObject.setMessage("No captcha, please try again after some time.");
dtObject.setSuccess(false);
}
} else {
dtObject.setMessage("Pls try again.");
dtObject.setSuccess(false);
}
}
} catch (Exception e) {
dtObject.setSuccess(false);
throw new ApplicationException(e.getMessage(), e);
}
return dtObject;
}
@Override
public ServiceObject update(ServiceObject dtObject) throws ApplicationException {
try {
customersDao.update((CustomersEntity) dtObject.getDataObject());
dtObject.setMessage("Record updated successfully.");
dtObject.setSuccess(true);
} catch (Exception e) {
dtObject.setSuccess(false);
throw new ApplicationException(e.getMessage(), e);
}
return dtObject;
}
While calling read(...) method, inside calling update(...) method two times, but result will be two different updates in single table with different row but it updated wrongly.
So, how can i use @Transactional annotation.
Kindly help me to solve this issue.