我试图将 JPA 中的删除级联到自引用表,但无论我做什么,我都会得到同样的错误。
@Entity
@Table(name="SYS_ACL", schema="dbo")
public class ACL implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected AccessControlListPK accessControlListPK;
@JoinColumn(name = "ROLE_ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private Roletable roletable;
@JoinColumn(name = "VIEW_ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private PageMap pagemap;
@Column(name="PARENT_ROLE")
private Long parentrole;
@Column(name="PARENT_VIEW_ID")
private Long parentview;
@JoinColumns({
@JoinColumn(name = "PARENT_ROLE", referencedColumnName = "ROLE_ID", nullable = true, insertable = false, updatable = false),
@JoinColumn(name = "PARENT_VIEW_ID", referencedColumnName = "VIEW_ID", nullable = true, insertable = false, updatable = false)})
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true)
@PrivateOwned
private List<ACL> children = new LinkedList<ACL>();
这是我得到的错误:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "SYSCCSSCNTRLLSTPRNTVWD". The conflict occurred in database "mydb", table "dbo.SYS_ACL" Error Code: 547
数据库结构是这样的:
CREATE TABLE SYS_ACL (
VIEW_ID NUMERIC,
ROLE_ID NUMERIC,
PARENT_ROLE NUMERIC,
PARENT_VIEW_ID NUMERIC,
CONSTRAINT PK PRIMARY KEY (ROLE_ID, VIEW_ID),
CONSTRAINT FK1 FORIEGN KEY (VIEW_ID) REFERENCING VIEW_TABLE(ID),
CONSTRAINT FK2 FORIEGN KEY (ROLE_ID) REFERENICNG ROLE_TABLE(ID),
CONSTRAINT FKSELF FOREIGN KEY (PARENT_ROLE, PARENT_VIEW) REFERENCING SYS_ACL(ROLE_ID, VIEW_ID));
我可以使用递归方法首先删除孩子,但我想知道在 JPA 中是否可以这样做。我正在使用 GF 3.1 和 Eclipselink 2.3.0