简体   繁体   中英

Parent entity getting unnecessarily updated when a new child entity is persisted

We are using JPA entities and hibernate for persistence. I have a Plan entity and an Escalation entity. 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. 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. 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. 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.

Please check if specifying cascade = CascadeType.PERSIST solves your issue

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

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