我在hibernate中使用连接表进行了多对多的映射,并且有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的方法。这可能是罐子问题吗?
答案 0 :(得分:0)
您是否已从Java代码或使用终端将其删除?如果您从终端删除它,它将无法正常工作。使用mysqldump备份后,您将看到该sql文件中没有ON DELETE SET NULL或ON DELETE CASCADE。这意味着它仅适用于Java代码。尝试从Java代码中删除它。我不确定如何删除它。而且我需要看到两个类才能看到代码。