1

我试图将 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

4

1 回答 1

0

什么是 SQL 日志?

您的模型似乎过于复杂。您可能想要删除 AccessControlListPK 并在 @ManyToOnes 上放置一个 @Id。

您的问题可能是因为您在@OneToMany 上使用@JoinColumn,通常您会使用mappedBy 和@ManyToOne 逆,您似乎使用@Basuc 作为外键,删除这些和OneToMany 上的连接列并为父级定义一个@ManyToOne。

于 2012-07-09T13:09:27.123 回答