孤儿删除约束违规

时间:2012-04-13 18:55:09

标签: jpa-2.0 eclipselink cascade orphan

我正在使用EclipseLink并拥有以下实体类(我尽可能地简化了场景,但在进一步简化后,错误不会发生):

@Entity
public class A {
    @Id
    @GeneratedValue
    private long id;
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval=true)
    private AbstractB b;
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AbstractB {
    @Id
    @GeneratedValue
    protected long id;   
}

@Entity
public class BImpl extends AbstractB {
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval=true)
    private D d;
}

@Entity
public class D {
    @Id
    @GeneratedValue
    private long id;
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @Size(min = 1)
    private Set<C> cs = new HashSet<C>();
}

@Entity
public class C {
    @Id
    @GeneratedValue
    private long id;
}

引用一个引用BImpl的实例,它引用一个D,而D又在一个集合中保存一个C的实例。

正如您在代码中看到的,所有引用都将orphanremoval设置为true和CascadeType.ALL。

当我将A-&gt; AbstractB的引用设置为null并合并回A的实例时,会发生以下约束违规:

20:05:03,388 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.388--ClientSession(2132156535)--Connection(1281180651)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--UPDATE A SET B_ID = ? WHERE (ID = ?)
20:05:03,388 INFO  [STDOUT]     bind => [null, 1]
20:05:03,390 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.39--ClientSession(2132156535)--Connection(1281180651)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--DELETE FROM BIMPL WHERE (ID = ?)
20:05:03,390 INFO  [STDOUT]     bind => [2]
20:05:03,391 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.391--ClientSession(2132156535)--Connection(1281180651)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--DELETE FROM ABSTRACTB WHERE (ID = ?)
20:05:03,391 INFO  [STDOUT]     bind => [2]
20:05:03,392 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.392--ClientSession(2132156535)--Connection(1281180651)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--DELETE FROM C WHERE (ID = ?)
20:05:03,393 INFO  [STDOUT]     bind => [4]
20:05:03,393 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.393--ClientSession(2132156535)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--SELECT 1
20:05:03,394 INFO  [STDOUT] [EL Warning]: 2012-04-13 20:05:03.394--UnitOfWork(2142376869)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
20:05:03,394 INFO  [STDOUT] Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`db`.`D_C`, CONSTRAINT `FK_D_C_cs_ID` FOREIGN KEY (`cs_ID`) REFERENCES `C` (`ID`))
20:05:03,394 INFO  [STDOUT] Error Code: 1451
20:05:03,395 INFO  [STDOUT] Call: DELETE FROM C WHERE (ID = ?)
20:05:03,395 INFO  [STDOUT]     bind => [4]

有人可以向我解释为什么在C实例之前不会删除D_C条目吗?

1 个答案:

答案 0 :(得分:1)

添加@CascadeOnDelete可以解决您的问题

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@CascadeOnDelete
@Size(min = 1)
public Set<TestChild> childrenSet = new HashSet<TestChild>();

另见:EclipseLink DeleteCascade

HTH Danny