【发布时间】: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