我正在使用JPA处理数据库备份引擎。一切都很好,但我遇到了解决这个问题的问题。我知道这与我如何定义实体有关。
首先,这是关于系统如何运作的简要说明:
要恢复基本上解压缩数据库并使备份成为源,将实时数据库作为目标(删除新目标数据库中的所有实体后)
一切正常,直到我注意到我从备份中遗漏了一张桌子。当我添加它时,它抱怨以下内容:
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`xinco`.`xinco_core_ace`, CONSTRAINT `FK_xinco_core_ace_xinco_core_user_id` FOREIGN KEY (`xinco_core_user_id`) REFERENCES `xinco_core_user` (`id`))
Error Code: 1451
Call: DELETE FROM xinco_core_user WHERE (id = ?)
bind => [1]
这是我收到错误时正在复制的实体:
package com.bluecubs.xinco.core.server.persistence;
import com.bluecubs.xinco.core.server.AuditedEntityListener;
import com.bluecubs.xinco.core.server.XincoAuditedObject;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.CascadeType;
/**
*
* @author Javier A. Ortiz Bultrón <javier.ortiz.78@gmail.com>
*/
@Entity
@Table(name = "xinco_core_ace")
@EntityListeners(AuditedEntityListener.class)
@NamedQueries({
@NamedQuery(name = "XincoCoreAce.findAll",
query = "SELECT x FROM XincoCoreAce x"),
@NamedQuery(name = "XincoCoreAce.findById",
query = "SELECT x FROM XincoCoreAce x WHERE x.id = :id"),
@NamedQuery(name = "XincoCoreAce.findByReadPermission",
query = "SELECT x FROM XincoCoreAce x WHERE x.readPermission = :readPermission"),
@NamedQuery(name = "XincoCoreAce.findByWritePermission",
query = "SELECT x FROM XincoCoreAce x WHERE x.writePermission = :writePermission"),
@NamedQuery(name = "XincoCoreAce.findByExecutePermission",
query = "SELECT x FROM XincoCoreAce x WHERE x.executePermission = :executePermission"),
@NamedQuery(name = "XincoCoreAce.findByAdminPermission",
query = "SELECT x FROM XincoCoreAce x WHERE x.adminPermission = :adminPermission")})
public class XincoCoreAce extends XincoAuditedObject implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "id", nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "read_permission", nullable = false)
private boolean readPermission;
@Basic(optional = false)
@Column(name = "write_permission", nullable = false)
private boolean writePermission;
@Basic(optional = false)
@Column(name = "execute_permission", nullable = false)
private boolean executePermission;
@Basic(optional = false)
@Column(name = "admin_permission", nullable = false)
private boolean adminPermission;
@JoinColumn(name = "xinco_core_data_id", referencedColumnName = "id", nullable = true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreData xincoCoreDataId;
@JoinColumn(name = "xinco_core_group_id", referencedColumnName = "id", nullable = true)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
private XincoCoreGroup xincoCoreGroupId;
@JoinColumn(name = "xinco_core_node_id", referencedColumnName = "id", nullable = true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreNode xincoCoreNodeId;
@JoinColumn(name = "xinco_core_user_id", referencedColumnName = "id", nullable = true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreUser xincoCoreUserId;
public XincoCoreAce() {
}
public XincoCoreAce(Integer id) {
this.id = id;
}
public XincoCoreAce(Integer id, boolean readPermission, boolean writePermission, boolean executePermission, boolean adminPermission) {
this.id = id;
this.readPermission = readPermission;
this.writePermission = writePermission;
this.executePermission = executePermission;
this.adminPermission = adminPermission;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public boolean getReadPermission() {
return readPermission;
}
public void setReadPermission(boolean readPermission) {
this.readPermission = readPermission;
}
public boolean getWritePermission() {
return writePermission;
}
public void setWritePermission(boolean writePermission) {
this.writePermission = writePermission;
}
public boolean getExecutePermission() {
return executePermission;
}
public void setExecutePermission(boolean executePermission) {
this.executePermission = executePermission;
}
public boolean getAdminPermission() {
return adminPermission;
}
public void setAdminPermission(boolean adminPermission) {
this.adminPermission = adminPermission;
}
public XincoCoreData getXincoCoreDataId() {
return xincoCoreDataId;
}
public void setXincoCoreDataId(XincoCoreData xincoCoreDataId) {
this.xincoCoreDataId = xincoCoreDataId;
}
public XincoCoreGroup getXincoCoreGroupId() {
return xincoCoreGroupId;
}
public void setXincoCoreGroupId(XincoCoreGroup xincoCoreGroupId) {
this.xincoCoreGroupId = xincoCoreGroupId;
}
public XincoCoreNode getXincoCoreNodeId() {
return xincoCoreNodeId;
}
public void setXincoCoreNodeId(XincoCoreNode xincoCoreNodeId) {
this.xincoCoreNodeId = xincoCoreNodeId;
}
public XincoCoreUser getXincoCoreUserId() {
return xincoCoreUserId;
}
public void setXincoCoreUserId(XincoCoreUser xincoCoreUserId) {
this.xincoCoreUserId = xincoCoreUserId;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof XincoCoreAce)) {
return false;
}
XincoCoreAce other = (XincoCoreAce) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.bluecubs.xinco.core.server.persistence.XincoCoreAce[id=" + id + "]";
}
}
我尝试更改xincoCoreDataId的级联类型,但它不起作用。我将继续努力,但欢迎任何反馈。
编辑: 似乎我复制了错误的异常。遗憾。
这是实际的异常
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL091217135308520' defined on 'XINCO_CORE_GROUP'.
Error Code: 20000
Call: INSERT INTO xinco_core_group (id, designation, status_number) VALUES (?, ?, ?)
bind => [1, general.group.admin, 1]
答案 0 :(得分:0)
我在这里看到的是数据库阻止您使用外键约束删除另一个表引用的记录。具体来说,表xinco_core_ace
看起来像一个名为xinco_core_user_id
的列,它是xinco_core_user
表中的外键。
除非先删除xinco_core_ace
中的行(参照完整性),否则无法删除该用户对象。
您可以在JPA映射中将此更改为从用户对象级联DELETE到您的XincoCoreAce对象,如果这是您想要的。
答案 1 :(得分:0)