Hibernate错误:无法删除或更新父行:外键约束失败

时间:2017-03-23 07:33:15

标签: java hibernate

我尝试了很多方法。但它总是错误! 我有两个实体类:Role.java和RolePrivilege, 这是Role.java:

package com.taxsys.nsfw.role.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="role")
public class Role implements Serializable{

    @Id
    @GenericGenerator(name="pk_uuid", strategy="uuid")
    @GeneratedValue(generator="pk_uuid")
    @Column(name="role_id")
    private String roleId;

    @Column(name="role_name",length=32, nullable=false)
    private String name;

    @Column(name="state", length=1)
    private String state;

    @OneToMany(targetEntity=RolePrivilege.class, 
    mappedBy="role", cascade=CascadeType.ALL,
    fetch=FetchType.EAGER)
    private Set<RolePrivilege> rolePrivileges = new HashSet<>();


    public static String ROLE_STATE_VALID = "1";
    public static String ROLE_STATE_INVALID = "0";


    public Set<RolePrivilege> getRolePrivileges() {
        return rolePrivileges;
    }
    public void setRolePrivileges(Set<RolePrivilege> rolePrivilege) {
        this.rolePrivileges = rolePrivilege;
    }
    public String getRoleId() {
        return roleId;
    }
    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public Role() {
    }
    public Role(String roleId, String roleName, String state, Set<RolePrivilege> rolePrivilege) {
        super();
        this.roleId = roleId;
        this.name = roleName;
        this.state = state;
        this.rolePrivileges = rolePrivilege;
    }
    public Role(String roleId) {
        this.roleId = roleId;
    }
}

这是RolePrivilege.java:

package com.taxsys.nsfw.role.entity;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;


@Entity
@Table(name="role_privilege")
public class RolePrivilege implements Serializable{

    @Column(name="role_privilege_id", length=32)
    @GenericGenerator(name="pk_uuid", strategy="uuid")
    @GeneratedValue(generator="pk_uuid")
    @Id
    private String id;


    @ManyToOne(targetEntity=Role.class, fetch=FetchType.EAGER)
    @JoinColumn(name="role_id", referencedColumnName="role_id")
    private Role role;

    private String code;

    public RolePrivilege(Role role, String code) {
        super();
        this.role = role;
        this.code = code;
    }

    public RolePrivilege() {
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((code == null) ? 0 : code.hashCode());
        result = prime * result + ((role == null) ? 0 : role.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;
        RolePrivilege other = (RolePrivilege) obj;
        if (code == null) {
            if (other.code != null)
                return false;
        } else if (!code.equals(other.code))
            return false;
        if (role == null) {
            if (other.role != null)
                return false;
        } else if (!role.equals(other.role))
            return false;
        return true;
    }

}

看看我的Role.java。属性&#39; cascade = CascadeType.ALL&#39;已指定。 角色代表我的程序中的主表。但是当我尝试从role.it错误中删除数据时!

15:14:29,905 ERROR [http-bio-8425-exec-4] JDBCExceptionReporter:234 - Cannot delete or update a parent row: a foreign key constraint fails (`itcasttax`.`role_privilege`, CONSTRAINT `FK45FBD62836249906` FOREIGN KEY (`role_id`) REFERENCES `role` (`role_id`))

但是在Role.java中,我指定了级联!我真的不明白为什么我仍然无法删除主表。

1 个答案:

答案 0 :(得分:0)

使用下面的代码。

@OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "role")
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.DELETE })
@Fetch(FetchMode.SELECT)
private Set<RolePrivilege> rolePrivileges