我正在尝试将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
答案 0 :(得分:0)
什么是SQL日志?
您的模型似乎过于复杂。你可能想要删除AccessControlListPK并在@ManyToOnes上放一个@Id。
您的问题可能是因为您在@OneToMany上使用@JoinColumn,通常您会使用mappedBy和@ManyToOne反转,您似乎使用@Basuc代替外键,删除这些和连接列在OneToMany上,只需为父母定义一个@ManyToOne。