JPA 2.0 Eclipselink - 添加/删除子列表获得乐观锁定

时间:2015-01-08 18:30:59

标签: jpa eclipselink spring-data-jpa

我遇到一个问题,即eclipselink(2.5)抛出一个OptimisticLockException,即使我修改的唯一一件事是尝试添加或删除子列表中的项目。

实体:

  @Entity
    @Table(name="PLAN_ORG_RELATIONSHIP")
    @Customizer(GridCacheCustomizer.class)
    @AdditionalCriteria("CURRENT_TIMESTAMP BETWEEN this.startDate AND this.endDate")
    @NamedQuery(name="PlanOrganizationRelationship.findAll", query="SELECT p FROM PlanOrganizationRelationship p")
    @Portable
    public class PlanOrganizationRelationship extends PrismObject implements Serializable {

    @OneToMany(mappedBy="planOrganizationRelationship", cascade=CascadeType.PERSIST,    orphanRemoval=true) 
    @PortableProperty(10)
    private List<PlanOrganizationAction> planOrganizationActions;

public PlanOrganizationAction addPlanOrganizationAction(PlanOrganizationAction planOrganizationAction) {
        getPlanOrganizationActions().add(planOrganizationAction);
        planOrganizationAction.setPlanOrgRelationship(this);
        return planOrganizationAction;
    }

    public PlanOrganizationAction removePlanOrganizationAction(PlanOrganizationAction planOrganizationAction) {
        getPlanOrganizationActions().remove(planOrganizationAction);
        planOrganizationAction.setPlanOrgRelationship(null);
        return planOrganizationAction;
    }

    @Column(name="LST_UPDT_DT")
    @Version
    @PortableProperty(5)
    private Timestamp lastUpdatedDate;
}

One To Many的另一面:

@Entity
@Table(name="PLAN_ORGANIZATION_ACTION")
@Customizer(GridCacheCustomizer.class)
@AdditionalCriteria("CURRENT_TIMESTAMP BETWEEN this.startDate AND this.endDate")
@NamedQuery(name="PlanOrganizationAction.findAll", query="SELECT p FROM PlanOrganizationAction p")
@Portable
public class PlanOrganizationAction extends PrismObject implements Serializable {
@ManyToOne
    @JoinColumn(name="PLN_ORG_RLTNP_SEQ_ID")
    @PortableProperty(7)
    private PlanOrganizationRelationship planOrganizationRelationship;
}

我有3条路径 - 添加与动作的新关系(两个实体都是新的)或添加动作或删除动作

当我添加两者时,我会坚持父母,孩子们也会坚持下去,这就是预期的行为。 当我尝试添加或删除时,我尝试类似

的内容
PrismOrganizationRelationship por = findById (..) //we are spring-data-jpa
por.addPlanOrganizationAction(action);
repo.save(por); // Throws optimistic lock - even though @Version is the same 

不确定导致此问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

检查cascade=CascadeType.PERSIST。它仅适用于持久化(保存新实体)操作。您的操作删除子实体意味着更新主实体。用于更新操作的s why, you need to use CascadeType.MARGE`。