OpenJPA删除父项时删除子项的错误顺序

OpenJPA 2.3.x的删除顺序发生在错误的顺序,我无法弄清楚原因.

鉴于这些JPA映射

// grandparent
@Entity
@Table(name = "three_phase_motor_input")
public class ThreePhaseMotorInput implements IThreePhaseMotorInput,Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Version
    private Integer version;

    @OnetoOne(orphanRemoval = true,cascade = CascadeType.ALL,optional = true,targetEntity = UnapprovedThreePhaseMotor.class)
    @JoinColumn(name = "unapproved_id")
    private IThreePhaseMotor unapprovedMotor;

// parent
@Entity
@Table(name = "unapproved_three_phase_motor")
public class UnapprovedThreePhaseMotor extends ThreePhaseMotor {
    @OnetoMany(orphanRemoval = true,fetch = FetchType.LAZY,targetEntity = UnapprovedThreePhaseWire.class)
    @JoinColumn(name = "motor_id",referencedColumnName = "id",nullable = false)
    @OrderColumn(name = "idx")
    private List<IThreePhaseWire> wires;


// child - Abstract
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Access(Accesstype.FIELD)
public abstract class ThreePhaseWire implements IThreePhaseWire,Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

// child concrete
@Entity
@Table(name = "unapproved_three_phase_wire")
public class UnapprovedThreePhaseWire extends ThreePhaseWire {

将祖父节中的unapprovedMotor设置为null时,日志文件中的sql语句顺序

572353115 UPDATE three_phase_motor_input SET status = ?,version = ?,approved_id = ?,unapproved_id = ? WHERE id = ? AND version = ? [params=(int) 1,(int) 8,(null) null,(long) 896,(int) 7]
 1683522521 DELETE FROM unapproved_three_phase_motor WHERE id = ? AND version = ? [params=(long) 209938,(int) 1]
 446470297 DELETE FROM unapproved_three_phase_wire WHERE id = ? [params=(long) 1394]

这会导致外键约束错误,因为在执行父删除语句时子项仍然存在.

解决方法

我从来没有使用过OpenJPA,但在我看来,问题可以通过级联安排来解决,你应该检查一下.

相关文章

应用场景 C端用户提交工单、工单创建完成之后、会发布一条工...
线程类,设置有一个公共资源 package cn.org.chris.concurre...
Java中的数字(带有0前缀和字符串)
在Java 9中使用JLink的目的是什么?
Java Stream API Filter(过滤器)
在Java中找到正数和负数数组元素的数量