Java 删除孤立项正在给出异常

Java 删除孤立项正在给出异常,java,hibernate,hibernate-mapping,all-delete-orphan,Java,Hibernate,Hibernate Mapping,All Delete Orphan,嗨,我有Department实体,在该Department实体中,我有字符串数据类型中的属性名Department name,另一个属性parentdepartment作为Department数据类型(类类型)作为该列的外键。我使用了一对一关系,级联类型为孤立-删除。如果删除我的父部门,我不想删除子部门,因此我在这里使用了孤立删除,但它给出了以下例外情况。 例外情况 SEVERE: Cannot delete or update a parent row: a foreign key const

嗨,我有Department实体,在该Department实体中,我有字符串数据类型中的属性名Department name,另一个属性parentdepartment作为Department数据类型(类类型)作为该列的外键。我使用了一对一关系,级联类型为孤立-删除。如果删除我的父部门,我不想删除子部门,因此我在这里使用了孤立删除,但它给出了以下例外情况。 例外情况

SEVERE: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`department`, CONSTRAINT `FKA9601F72A15DD2FD` FOREIGN KEY (`parentdeparment_id`) REFERENCES `department` (`id`))
Mar 14, 2014 9:48:49 AM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at com.myapp.struts.Teast.main(Teast.java:20)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`department`, CONSTRAINT `FKA9601F72A15DD2FD` FOREIGN KEY (`parentdeparment_id`) REFERENCES `department` (`id`))
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2028)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
    ... 8 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`department`, CONSTRAINT `FKA9601F72A15DD2FD` FOREIGN KEY (`parentdeparment_id`) REFERENCES `department` (`id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1980)
    ... 11 more

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at com.myapp.struts.Teast.main(Teast.java:20)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`department`, CONSTRAINT `FKA9601F72A15DD2FD` FOREIGN KEY (`parentdeparment_id`) REFERENCES `department` (`id`))
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2028)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
    ... 8 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`department`, CONSTRAINT `FKA9601F72A15DD2FD` FOREIGN KEY (`parentdeparment_id`) REFERENCES `department` (`id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1980)
    ... 11 more
Java Result: 1
部门实体

  /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
//import org.hibernate.annotations.Cascade;
//import org.hibernate.annotations.CascadeType;

/**
 *
 * @author hyva
 */
@Entity
public class Department implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String departmaentname;
    private String daperthead;
    @OneToMany
    @Cascade(CascadeType.DELETE_ORPHAN)
    private Set<Department> parentdeparment = new HashSet<Department>();

    public boolean addDepartment(Department d) {
        return parentdeparment.add(d);
    }

    public String getDepartmaentname() {
        return departmaentname;
    }

    public void setDepartmaentname(String departmaentname) {
        this.departmaentname = departmaentname;
    }

    public String getDaperthead() {
        return daperthead;
    }

    public void setDaperthead(String daperthead) {
        this.daperthead = daperthead;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @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 Department)) {
            return false;
        }
        Department other = (Department) 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.myapp.struts.Department[ id=" + id + " ]";
    }

    public Set<Department> getParentdeparment() {
        return parentdeparment;
    }

    public void setParentdeparment(Set<Department> parentdeparment) {
        this.parentdeparment = parentdeparment;
    }
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包com.myapp.struts;
导入java.io.Serializable;
导入java.util.HashSet;
导入java.util.Set;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.OneToMany;
导入javax.persistence.OneToOne;
导入org.hibernate.annotations.Cascade;
导入org.hibernate.annotations.CascadeType;
//导入org.hibernate.annotations.Cascade;
//导入org.hibernate.annotations.CascadeType;
/**
*
*@作者海娃
*/
@实体
公共类部门实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
私有字符串departmaentname;
私用线头;
@独身癖
@级联(级联类型。删除\u孤立)
private Set parentdeparment=new HashSet();
公共服务部(d部){
返回父母部门。添加(d);
}
公共字符串getDepartmaentname(){
返回部门名称;
}
public void setDepartmaentname(字符串departmaentname){
this.departmaentname=departmaentname;
}
公共字符串getDaperhead(){
回程刀头;
}
公共void设置数据头(字符串数据头){
this.daperthead=daperthead;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
@凌驾
公共int hashCode(){
int hash=0;
hash+=(id!=null?id.hashCode():0);
返回散列;
}
@凌驾
公共布尔等于(对象){
//TODO:警告-如果未设置id字段,此方法将不起作用
如果(!(部门的对象实例)){
返回false;
}
部门其他=(部门)对象;
如果((this.id==null&&other.id!=null)| |(this.id!=null&&!this.id.equals(other.id))){
返回false;
}
返回true;
}
@凌驾
公共字符串toString(){
返回“com.myapp.struts.Department[id=“+id+”]”;
}
公共集getParentdeparment(){
返回父母部门;
}
公共无效setParentdeparment(Set parentdeparment){
this.parentdeparment=parentdeparment;
}
}

这里有些问题,一个部门是否只有一个上级部门?如果要删除parentdepartment,请首先删除department表中的所有引用,我认为许多其他行都引用了要删除的parentdepartment,这可能是原因所在。如果任何其他行具有此parentdept id,请尝试查看department表。如果是,您可能必须使用多对一关系而不是一关系。我已更新了我的代码,请参见上文。我正在尝试取消对您的数据模型的理解。他所在部门是否有多名家长?或者是另一种方式?这里没有一个子部门有一个父部门。但家长部门会有很多孩子,就像一个班有很多学生一样。