简体   繁体   English

休眠@Transactional

[英]Hibernate @Transactional

I am new for hibernate, how to use @Transactional in spring, 我是休眠的新手,如何在春季使用@Transactional,

this is inf class, 这是INF课,

@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. 在调用read(...)方法时,在内部调用了update(...)方法两次,但结果将是在具有不同行的单个表中进行两次不同的更新,但更新错误。

So, how can i use @Transactional annotation. 因此,如何使用@Transactional批注。

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. 如果这就是您所说的错误,那么请考虑@Transactional批注定义了附加到持久性上下文的单个事务的范围。

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. 因此,当您使用相同的对象(设置或更改)时,假设该对象的属性不同,则基础JPA会检测到它基本上就是您要更新的同一对象。 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. 因此,不是运行两个更新查询,而是运行一个最新/最后一个更新语句,并且只有该更改反映在数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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