java - 删除时 CASCADE 在 hibernate 中的多对多映射中不起作用

标签 java hibernate hibernate-mapping

我在 hibernate 中有多对多映射,带有连接表,并且有 3 个不同的类。 OperationEntityEndPointEntity 具有manyToMany 映射。我尝试使用 @Cascade(org.hibernate.annotations.CascadeType.ALL)@ManyToOne(cascade=CascadeType.ALL) 注释。但是在数据库中 checkin 时,删除和更新时都是 RESTRICT。下面是代码:

OperationEntity.java

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="t_operation", schema="test")
public class OperationEntity implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="op_id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int opId;

    @Column(name="op_name")
    private String opName;

    @Column(name="op_desc")
    private String opDesc;

    @OneToMany(mappedBy="operationsEntity")
    private List<OpEndPointEntity> listOfOperationsEndpoints;   

    public int getOpId() {
        return opId;
    }

    public void setOpId(int opId) {
        this.opId = opId;
    }

    public String getOpName() {
        return opName;
    }

    public void setOpName(String opName) {
        this.opName = opName;
    }

    public String getOpDesc() {
        return opDesc;
    }

    public void setOpDesc(String opDesc) {
        this.opDesc = opDesc;
    }

    public List<OpEndPointEntity> getListOfOperationsEndpoints() {
        return listOfOperationsEndpoints;
    }

    public void setListOfOperationsEndpoints(
            List<OpEndPointEntity> listOfOperationsEndpoints) {
        this.listOfOperationsEndpoints = listOfOperationsEndpoints;
    }

}


    EndPointEntity.java

    import java.io.Serializable;
    import java.util.List;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;

    @Entity
    @Table(name="t_endPoint", schema="test")
    public class EndPointEntity implements Serializable {

        private static final long serialVersionUID = 1L;

        @Id
        @Column(name="end_point_id")
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int endPointId;

        @Column(name="end_point")
        private String endPoint;

        @Column(name="end_point_desc")
        private String endPointDesc;

        @OneToMany(mappedBy="endPointEntity")
        private List<OpEndPointEntity> listOpEndPoint;

        public int getEndPointId() {
            return endPointId;
        }

        public void setEndPointId(int endPointId) {
            this.endPointId = endPointId;
        }

        public String getEndPoint() {
            return endPoint;
        }

        public void setEndPoint(String endPoint) {
            this.endPoint = endPoint;
        }

        public String getEndPointDesc() {
            return endPointDesc;
        }

        public void setEndPointDesc(String endPointDesc) {
            this.endPointDesc = endPointDesc;
        }

        public List<OpEndPointEntity> getListOpEndPoint() {
            return listOpEndPoint;
        }

        public void setListOpEndPoint(List<OpEndPointEntity> listOpEndPoint) {
            this.listOpEndPoint = listOpEndPoint;
        }
    }


    Mapping class : OpEndPointEntity.java

    import java.io.Serializable;

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

    import org.hibernate.annotations.Cascade;

    @Entity
    @Table(name="t_op_endpoint_map", schema="test")
    public class OpEndPointEntity implements Serializable {
        private static final long serialVersionUID = 71L;

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="OP_ENDPOINT_ID")
        private Integer operationEndpointId;

        @ManyToOne(cascade=CascadeType.ALL)
        @Cascade(org.hibernate.annotations.CascadeType.ALL)
        @JoinColumn(name="end_point_id")
        private EndPointEntity endPointEntity;

        @ManyToOne
        @Cascade(org.hibernate.annotations.CascadeType.ALL)
        @JoinColumn(name="op_id")
        private OperationEntity operationsEntity;

        public Integer getOperationEndpointId() {
            return operationEndpointId;
        }

        public void setOperationEndpointId(Integer operationEndpointId) {
            this.operationEndpointId = operationEndpointId;
        }

        public EndPointEntity getEndPointEntity() {
            return endPointEntity;
        }

        public void setEndPointEntity(EndPointEntity endPointEntity) {
            this.endPointEntity = endPointEntity;
        }

        public OperationEntity getOperationsEntity() {
            return operationsEntity;
        }

        public void setOperationsEntity(OperationEntity operationsEntity) {
            this.operationsEntity = operationsEntity;
        }

    }

请提供一种在删除和更新时进行级联的方法。会不会是jar的问题?

最佳答案

您是否从 Java 代码或使用终端删除了它?如果您从终端删除它,它将不起作用。使用 mysqldump 备份后,您将看到该 sql 文件中没有 ON DELETE SET NULL 或 ON DELETE CASCADE 。这意味着它仅适用于 Java 代码。尝试从 Java 代码中删除它。我不知道你怎么删除它。我需要查看这两个类才能查看代码。

关于java - 删除时 CASCADE 在 hibernate 中的多对多映射中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39871030/

相关文章:

java - 整数对添加到哈希集java

java - 在H2中, "TRACE_LEVEL_FILE=4"仅适用于本地/嵌入式数据库吗?

java - HIbernate映射异常: PropertyNotFoundException: Could not find a setter

java - Ubuntu 32位下编译LuaJava

java - 无法使用 Hibernate Spatial 将点插入 mysql 数据库

java.lang.UnsupportedOperationException : Unrecognized property type: org. hibernate.type.BagType

postgresql - 通过计算不同的列值来限制查询

java - hibernate 异常 : A collection with cascade ="all-delete-orphan" was no longer referenced by the owning entity instance

java - hibernate 映射两个表失败

java - RDD 的最后一项未保存到 HDFS