OpenJPA删除父级时,删除子级的顺序错误 [英] OpenJPA Wrong order for delete of child when deleting parent

查看:104
本文介绍了OpenJPA删除父级时,删除子级的顺序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenJPA 2.3.x的删除顺序顺序错误,我不知道为什么.

The order of deletes for OpenJPA 2.3.x are happening in the wrong order and I can't figure out why.

给出这些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, cascade = CascadeType.ALL, 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语句的顺序

Order of SQL statements in log file when setting unapprovedMotor in grandparent to null

 572353115 UPDATE three_phase_motor_input SET status = ?, version = ?, approved_id = ?, unapproved_id = ? WHERE id = ? AND version = ? [params=(int) 1, (int) 8, (null) null, (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]

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

This causes a foreign key constraint error since the child still exists when the parent delete statement is executed.

推荐答案

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

I never used OpenJPA but it seems to me that problem can be solved with arrangement on cascading you should check that.

这篇关于OpenJPA删除父级时,删除子级的顺序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆