简体   繁体   English

当持久保存新的子实体时,父实体会不必要地更新

[英]Parent entity getting unnecessarily updated when a new child entity is persisted

We are using JPA entities and hibernate for persistence. 我们使用JPA实体和hibernate来实现持久性。 I have a Plan entity and an Escalation entity. 我有一个Plan实体和一个Escalation实体。 When I create a new escalation and persist it, the plan is also somehow getting updated. 当我创建一个新的升级并坚持下去时,该计划也会以某种方式得到更新。 This update is causing OptimisticLockException and preventing further escalations from getting persisted. 此更新导致OptimisticLockException并阻止进一步的升级持久化。 Here's the code skeleton - 这是代码骨架 -

@Entity
@Table(name = "T_ESCLT")
public class Escalation extends PersistentEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ESCLT_ID")
    private Integer id;

    @ReflectionCopy.Exclude
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CUST_RNEW_TASK_ID", nullable = false)
    private CustomerRenewalTask renewalTask;

    @ReflectionCopy.Exclude
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PLN_DSG_ID")
    private Plan plan;

public Escalation(CustomerRenewalTask task, Plan plan, String description) {
        Preconditions.checkNotNull(task);
        Preconditions.checkNotNull(description);

        this.renewalTask = task;
        this.plan = plan;
        this.description = description;
        this.creationTimestamp = DateUtils.currentTimestamp();
    }

Plan and CustomerRenewalTask do not have escalation mapped in them. PlanCustomerRenewalTask没有映射升级。 When I run this 当我跑这个

@Transactional
    public Result persist() {
        CustomerRenewalTask customerRenewalTask = customerRenewalTaskDao.findById(2);
        Plan plan = planDao.findById(16);
        planDao.detach(plan);
        Escalation escalation = new Escalation(customerRenewalTask, plan, "My Escalation");
        escalationDao.persist(escalation);
        return ok();
    }

I see this in the console log 我在控制台日志中看到了这一点

DEBUG - insert into T_ESCLT (ESCLT_ID, OPTMSTC_LOCK_ID, ATRB_NM, CMNT_TXT, CRT_TS, ESCLT_DSCR, APP_LNK_TXT, PLN_DSG_ID, RT_BLCK_IND, CUST_RNEW_TASK_ID, RSLV_DT, RSLV_BY_USR_ID, RSLV_BY_USR_NM, ESCLT_STTS_CD, ESCLT_TYP_CD) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
DEBUG - update T_PLN_DSG set OPTMSTC_LOCK_ID=?, ASSOC_PLN_DSG_ID=?, BRTH_DT_RT_IND=?, PLN_EFF_DT=?, ELGBL_MBR_CNT=?, RNEW_PLN_DTL_XML=?, SUM_MBR_PRTCP_LIF_CNT=?, VLD_STTS_CD=?, PLN_NM=?, PLN_GRP_ID=?, PRNT_PLN_DSG_ID=?, PRTCP_MBR_CNT=?, PRTCP_PCT=?, PRTNR_PLN_DSG_ID=?, RT_CALC_XML=?, UW_VRFY_IND=?, SUM_VOL_AMT=? where PLN_DSG_ID=? and OPTMSTC_LOCK_ID=?

I do not want the update on plan to be issued as nothing on Plan got changed. 我不希望计划的更新发布,因为Plan没有改变。 I just used plan to create an escalation. 我刚刚使用计划来创建升级。

One way to stop this updates from happening is to specify update=false for individual properties of Plan Entity, but by doing this you prevent updates happening altogether on the Plan table. 阻止此更新发生的一种方法是为Plan Entity的各个属性指定update = false ,但这样做可以防止在Plan表上完全发生更新。

Please check if specifying cascade = CascadeType.PERSIST solves your issue 请检查指定cascade = CascadeType.PERSIST是否解决了您的问题

@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST)

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

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