Hibernate@Transaction?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (81)

这是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;

执行类,

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;
}

在调用read(.)方法时,内部调用UPDATE(.)方法两次,但结果将是具有不同行但更新错误的单个表中的两个不同的更新。

那么,我如何使用@Transaction注释。

提问于
用户回答回答于

它是否更新了同一行而不是两行?如果这就是你所说的错误那么请考虑@Transactional注释定义附加到持久化上下文的单个事务的范围。

因此,当你使用相同的对象,设置或更改让我们说对象的不同属性时,基础JPA会检测到它与你尝试更新的对象基本相同,因此只有一个运行,而不是两个更新查询,这是最新/最后一次更新声明,只有该更改才会反映在数据库中。

所属标签

可能回答问题的人

  • 应用案例分享

    1 粉丝490 提问19 回答
  • 学生

    3 粉丝476 提问13 回答
  • uncle_light

    5 粉丝518 提问12 回答
  • 最爱开车啦

    8 粉丝503 提问12 回答

扫码关注云+社区

领取腾讯云代金券