我正在使用 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;
}
A 的实例引用了一个 BImpl,它引用了一个 D,而 D 又在一个集合中保存了一个 C 的实例。
正如您在代码中看到的,所有引用都将 orphanremoval 设置为 true 和 CascadeType.ALL。
当我将 A->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]
谁能向我解释一下,为什么 D_C 条目在 C 实例之前没有被删除?