简体   繁体   中英

Hibernate @Transactional

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.

Did it update the same row instead of two different rows? If that's what you are saying to be wrong then please consider that the @Transactional annotation defines the scope of a single transaction attached to a persistence context.

So when you use the same object, set or change, let's say different properties of the object, underlying JPA detects that it's basically the same object you are trying to update. So instead of two update queries only one runs which is the latest/last update statement and only that change gets reflected in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM