简体   繁体   English

Hibernate集合映射问题。无法删除或更新父行:外键约束失败

[英]Hibernate collections mapping questions.Cannot delete or update a parent row: a foreign key constraint fails

In Hibernate, it may have no the problem about cascade in collections mapping.but l encountered.l use Annotation in Hibernate 4.2. 在Hibernate中,可能没有关于集合映射中级联的问题。但是遇到了。在Hibernate 4.2中使用了Annotation。

this is my entity class: 这是我的实体类:

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;

import com.partysys.partymanage.deus.entity.Deus;
import com.partysys.partymanage.period.entity.Period;
import com.partysys.sysmanage.branch.entity.Branch;


@Entity
@Table(name = "partymember")

public class Partymember implements java.io.Serializable {

    private String id;

    private String password;

    private String name;

List<String> cultivate;
    public Partymember() {
    }

    public Partymember(String id) {
        super();
        this.id = id;
    }



    @GenericGenerator(name = "generator", strategy = "uuid.hex")
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "partymember_id", unique = true, nullable = false, length = 32)
    public String getId() {
        return this.id;
    }


    public void setId(String id) {
        this.id = id;
    }
    @Column(name="password", length=25)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }




    @Column(name = "name", length = 30, nullable=false)

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }




 @ElementCollection(targetClass=String.class, fetch=FetchType.EAGER)
 @CollectionTable(name="cultivate_person",joinColumns=@JoinColumn(name="partymember_id",nullable=false))
        @Column(name = "cultivate", length = 20)

 @OrderColumn(name="t_order")
    public List<String> getCultivate() {
        return this.cultivate;
    }

    public void setCultivate(List<String> cultivate) {
        this.cultivate = cultivate;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Partymember other = (Partymember) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

}

but when l try to delete data in partymember, it mistakes. 但是当我尝试删除党员中的数据时,会出错。

BaseDaoImpl.java: BaseDaoImpl.java:

public void delete(Serializable id)
    {
        getSessionFactory().getCurrentSession()
            .createQuery("delete " + clazz.getSimpleName()
                + " en where en.id = ?0")
            .setParameter("0" , id)
            .executeUpdate();
    }

PartymemberAction.java: PartymemberAction.java:

partymemberService.delete(partymember.getId());

PartymemberServiceImpl.java: PartymemberServiceImpl.java:

@Override
    public void delete(Serializable id) {
        partymemberDao.deleteUserRoleByUserId(id);
        partymemberDao.delete(id);
    }

the stack trace: 堆栈跟踪:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`partysys`.`cultivate_person`, CONSTRAINT `FK_sllcrigxee4qhp11422d59mop` FOREIGN KEY (`partymember_id`) REFERENCES `partymember` (`partymember_id`))

but when l delete the data in Partymember, why it doesn't delete the data in cultivate(collections mapping) ? 但是当我删除Partymember中的数据时,为什么不删除培养(集合映射)中的数据呢? it is so strange! 太奇怪了! Thank you! 谢谢!

Your child table have foreign key, you have not enabled cascade delete. 您的子表具有外键,尚未启用级联删除。

So when you are trying to delete parent row, child rows are not going to deleted, that's why you are facing MySQLIntegrityConstraintViolationException. 因此,当您尝试删除父行时,子行不会被删除,这就是为什么您面临MySQLIntegrityConstraintViolationException的原因。

You should enable cascade delete to delete all dependent child rows on deletion of parent row. 您应该启用级联删除功能,以在删除父行时删除所有相关的子行。

The syntax will be something like below 语法如下所示

@OneToMany(mappedBy="parent_table", cascade={CascadeType.PERSIST, CascadeType.REMOVE}) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 休眠:无法删除或更新父行:外键约束失败 - Hibernate :Cannot delete or update a parent row: a foreign key constraint fails 休眠错误:无法删除或更新父行:外键约束失败 - Hibernate error:Cannot delete or update a parent row: a foreign key constraint fails Hibernate 抛出无法删除或更新父行:外键约束失败 - Hibernate throws Cannot delete or update a parent row: a foreign key constraint fails 无法删除或更新父行:外键约束在Hibernate create-drop上失败 - Cannot delete or update a parent row: a foreign key constraint fails on Hibernate create-drop Hibernate引发-无法删除或更新父行:外键约束失败 - Hibernate throws - Cannot delete or update a parent row: a foreign key constraint fails Hibernate 错误:无法删除或更新父行:外键约束失败 - Hibernate ERROR: Cannot delete or update a parent row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - hibernate 中的双向映射 - Cannot add or update a child row: a foreign key constraint fails - Bidirectional mapping in hibernate Hibernate:无法添加或更新子行:外键约束失败 - Hibernate :Cannot add or update a child row: a foreign key constraint fails Hibernate 无法添加或更新子行:外键约束失败 - Hibernate Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败Hibernate - Cannot add or update a child row: a foreign key constraint fails Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM