我没有包含与此问题无关的列。我将简要描述我想要做什么,然后继续解释问题。我正在尝试在数据库中建模一个图形。每个顶点有多个边(Vertex->Edge:一对多),每个顶点物理Id映射到一个唯一的vertexId(一对一)。虚拟 vertexIds 和 physicalResourceIds 之间的这种映射是为了另一个目的。
简而言之,我的问题是我想在删除顶点时将删除级联到边,但我不想在删除边时将删除级联到顶点。我有一个包含三个表的数据库,它们的模型类如下所示:
public class VertexModel implements java.io.Serializable{
/**
* @return the mapping
*/
@OneToOne(mappedBy = "vertex", fetch = FetchType.EAGER, cascade = {CascadeType.ALL} )
public PhysicalResourceToVertexMappingModel getMapping() {
return mapping;
}
/**
* @param mapping the mapping to set
*/
public void setMapping(PhysicalResourceToVertexMappingModel mapping) {
this.mapping = mapping;
}
/**
* @return the edgeSet
*/
@OneToMany(mappedBy = "sourceVertex", fetch = FetchType.EAGER, cascade = {CascadeType.ALL} )
public Set<EdgeModel> getSourceEdgeSet() {
return sourceEdgeSet;
}
/**
* @param edgeSet the edgeSet to set
*/
public void setSourceEdgeSet(Set<EdgeModel> sourceEdgeSet) {
this.sourceEdgeSet = sourceEdgeSet;
}
/**
* @return the destinationEdgeSet
*/
@OneToMany(mappedBy = "destinationVertex", fetch = FetchType.EAGER, cascade = {CascadeType.ALL} )
public Set<EdgeModel> getDestinationEdgeSet() {
return destinationEdgeSet;
}
/**
* @param destinationEdgeSet the destinationEdgeSet to set
*/
public void setDestinationEdgeSet(Set<EdgeModel> destinationEdgeSet) {
this.destinationEdgeSet = destinationEdgeSet;
}
public class EdgeModel implements java.io.Serializable {
/**
* Gets the source vertex's vertex
* @return the sourceVertex
*/
@ManyToOne(targetEntity = VertexModel.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "SOURCE_VERTEX_ID", nullable = false)
public VertexModel getSourceVertex() {
return this.sourceVertex;
}
/**
* Sets the source vertex
* @param sourceVertex the sourceVertex to set
*/
public void setSourceVertex(VertexModel sourceVertex) {
this.sourceVertex = sourceVertex;
}
/**
* Gets the destination vertex
* @return the destinationVertex
*/
@ManyToOne(targetEntity = VertexModel.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "DESTINATION_VERTEX_ID", nullable = false)
public VertexModel getDestinationVertex() {
return this.destinationVertex;
}
/**
* Sets the destination vertex
* @param destinationVertex
*/
public void setDestinationVertex(VertexModel destinationVertex) {
this.destinationVertex = destinationVertex;
}
public class PhysicalResourceToVertexMappingModel implements java.io.Serializable{
/**
* @return the vertex
*/
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="VERTEX_ID")
public VertexModel getVertex() {
return vertex;
}
/**
* @param vertex the vertex to set
*/
public void setVertex(VertexModel vertex) {
this.vertex = vertex;
}
现在,我的问题是,当我删除一个顶点时,该顶点上的所有边缘也会被删除,这是可取的。但是,接下来发生的事情是不可取的。当我删除一条边时,所有具有该边的顶点都会被删除,然后这些顶点会删除它们上的边事件,这会导致其他顶点被删除,依此类推。基本上,父子关系的映射不够好。
简而言之,如果这是图: V1 (E1) V2 (E2) V3 (E3) V4 其中 V 是顶点,E 是边。现在,当我删除 V1 时,E1 会被删除,这是预期的。但是随后,V2 也被删除(因为 E1 被删除),然后,V2 被删除,依此类推,整个图也被删除。
我在这里做错了什么?我的父子映射(使用@mappedBy)错了吗?我的删除代码如下:
Session session = this.getCurrentSession();
Criteria criteriaVertex = session.createCriteria(VertexModel.class);
Criteria criteriaMapper = session.createCriteria(PhysicalResourceToVertexMappingModel.class);
criteriaMapper.createAlias("vertex", "vertex")
.add(Restrictions.eq("vertex.clientId", clientId))
.add(Restrictions.eq("vertex.nodeId", nodeId ))
.add(Restrictions.eq(Constants.Columns.PHYSICAL_ID, physicalId));
PhysicalResourceToVertexMappingModel resultMapping = (PhysicalResourceToVertexMappingModel) criteriaMapper.uniqueResult();
String vertexId = resultMapping.getVertex().getVertexId();
criteriaVertex.add(Restrictions.eq(Constants.Columns.VERTEX_ID, vertexId));
VertexModel resultVertex = (VertexModel) criteriaVertex.uniqueResult();
//resultVertex.getSourceEdgeSet().clear();
//resultVertex.getDestinationEdgeSet().clear();
session.delete(resultMapping);
session.delete(resultVertex);
我试图删除一些 StackOverflow 用户在其他问题上提到的实体,但这引发了这个异常:org.hibernate.ObjectDeletedException: deleted object will be re-save by cascade (remove deleted object from associations)
如何进行删除(级联或模型父子关系),以便当我删除顶点时,所有边事件都会被删除,但是当我删除边时,不会删除任何顶点?
编辑:由于没有回复,我只想澄清问题很简单,我想对父子关系建模,这样删除子项不会删除父项。(如果除此之外我还获得了添加孩子将导致在父母中保存的功能,那就更好了)。我该怎么做呢?我的注释错了吗?谢谢