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

前端之家收集整理的这篇文章主要介绍了OpenJPA删除父项时删除子项的错误顺序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
OpenJPA 2.3.x的删除顺序发生在错误的顺序,我无法弄清楚原因.

鉴于这些JPA映射

  1. // grandparent
  2. @Entity
  3. @Table(name = "three_phase_motor_input")
  4. public class ThreePhaseMotorInput implements IThreePhaseMotorInput,Serializable {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. private Long id;
  8. @Version
  9. private Integer version;
  10.  
  11. @OneToOne(orphanRemoval = true,cascade = CascadeType.ALL,optional = true,targetEntity = UnapprovedThreePhaseMotor.class)
  12. @JoinColumn(name = "unapproved_id")
  13. private IThreePhaseMotor unapprovedMotor;
  14.  
  15. // parent
  16. @Entity
  17. @Table(name = "unapproved_three_phase_motor")
  18. public class UnapprovedThreePhaseMotor extends ThreePhaseMotor {
  19. @OneToMany(orphanRemoval = true,fetch = FetchType.LAZY,targetEntity = UnapprovedThreePhaseWire.class)
  20. @JoinColumn(name = "motor_id",referencedColumnName = "id",nullable = false)
  21. @OrderColumn(name = "idx")
  22. private List<IThreePhaseWire> wires;
  23.  
  24.  
  25. // child - Abstract
  26. @Entity
  27. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  28. @Access(AccessType.FIELD)
  29. public abstract class ThreePhaseWire implements IThreePhaseWire,Serializable {
  30. @Id
  31. @GeneratedValue(strategy = GenerationType.IDENTITY)
  32. private Long id;
  33.  
  34. // child concrete
  35. @Entity
  36. @Table(name = "unapproved_three_phase_wire")
  37. public class UnapprovedThreePhaseWire extends ThreePhaseWire {

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

  1. 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]
  2. 1683522521 DELETE FROM unapproved_three_phase_motor WHERE id = ? AND version = ? [params=(long) 209938,(int) 1]
  3. 446470297 DELETE FROM unapproved_three_phase_wire WHERE id = ? [params=(long) 1394]

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

解决方法

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

猜你在找的Java相关文章