【问题标题】:On delete CASCADE is not working in manyToMany mapping in hibernate在删除 CASCADE 在休眠中的多对多映射中不起作用
【发布时间】:2016-10-05 10:03:13
【问题描述】:

我在休眠中使用连接表进行多对多映射,并且有 3 个不同的类。 OperationEntity 和 EndPointEntity 具有 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。会是jar问题吗?

【问题讨论】:

  • 尝试将orphanRemoval = true 添加到@OneToMany 注释中。
  • 它不适用于 orphanRemoval = true

标签: java hibernate hibernate-mapping


【解决方案1】:

您是从 Java 代码中删除它还是使用终端?如果您从终端中删除它,它将无法正常工作。使用 mysqldump 备份后,您将看到该 sql 文件中没有 ON DELETE SET NULL 或 ON DELETE CASCADE。这意味着它仅适用于 Java 代码。尝试从 Java 代码中删除它。我不确定你是如何删除它的。而且我需要查看这两个类才能看到代码。

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2011-04-07
    相关资源
    最近更新 更多