簡體   English   中英

在刪除時CASCADE在hibernate中的manyToMany映射中不起作用

[英]On delete CASCADE is not working in manyToMany mapping in hibernate

我在hibernate中使用連接表進行多對多映射,並且有3個不同的類。 OperationEntityEndPointEntity具有manyToMany映射。 我嘗試使用@Cascade(org.hibernate.annotations.CascadeType.ALL)@ManyToOne(cascade=CascadeType.ALL)注釋。 但是在數據庫中檢查時,刪除和更新是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;
        }

    }

請提供一種刪除和更新CASCADE的方法。 罐頭問題可以嗎?

您是從Java代碼中刪除它還是使用終端? 如果從終端刪除它,它將無法正常工作。 使用mysqldump備份后,您將看到該sql文件中沒有ON DELETE SET NULL或ON DELETE CASCADE。 這意味着它只適用於Java代碼。 嘗試從Java代碼中刪除它。 我不確定你是怎么刪除它的。 我需要看兩個類來查看代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM