Hibernate多对多删除
当内容与另一个对象存在多对多关系时,我在删除内容时遇到一些麻烦。
为了执行删除,我有一个命名查询数组,要删除,我循环遍历该数组并执行每个实体的删除,如下所示:
private static final String[] DELETE_CONTENT_QUERY_NAMES = {
"Entity1.deleteByContentId",
"Entity2.deleteByContentId",
"Entity3.deleteByContentId",
"Entity4.deleteByContentId",
"Entity5.deleteByContentId",
"EntityWithManyToMany.deleteByContentId",
"Entity7.deleteByContentId",
"Content.DeleteByContent"
};
@Override
@Transactional
public void deleteContent(Content content) throws Exception{
Map<String, Object> params = new HashMap<String, Object>();
params.put("contentId", content.getContentId());
for (String queryName : DELETE_CONTENT_QUERY_NAMES) {
dao.batchDeleteByNamedQuery(queryName, params);
}
}
当迭代尝试执行 EntityWithManyToMany.deleteByContentId HQL 批量查询时,我可以在日志上看到翻译后的sql指令,但后面跟着一组同一行的插入,MySQL正确地返回了一个约束错误。
这是我尝试删除的多对多关系对象:
@Entity
@Embeddable
public class CategoryContent implements java.io.Serializable {
/***
* Private Declarations
*/
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "contentId", column = @Column(
name = "CONTENT_ID",
nullable = false, precision = 10, scale = 0)),
@AttributeOverride(name = "categoryId", column = @Column(
name = "CATEGORY_ID",
nullable = false, precision = 10, scale = 0))
})
@NotNull
private CategoryContentId id;
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name = "CATEGORY_ID", insertable=false, updatable=false)
private Category category;
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name = "CONTENT_ID", insertable=false, updatable=false)
private Content content;
@Column(name = "PRIORITY", nullable = true)
private Integer priority;
}
您认为问题可能与 CascadeType 有关吗?我该如何解决这个问题?
任何建议表示赞赏!
多谢, 达维德.
I have some trouble on deleting a content when is present a many to many relation with another object.
To perform delete I have an array of named query and to delete, I loop through the array and perform delete, entity per entity, like this:
private static final String[] DELETE_CONTENT_QUERY_NAMES = {
"Entity1.deleteByContentId",
"Entity2.deleteByContentId",
"Entity3.deleteByContentId",
"Entity4.deleteByContentId",
"Entity5.deleteByContentId",
"EntityWithManyToMany.deleteByContentId",
"Entity7.deleteByContentId",
"Content.DeleteByContent"
};
@Override
@Transactional
public void deleteContent(Content content) throws Exception{
Map<String, Object> params = new HashMap<String, Object>();
params.put("contentId", content.getContentId());
for (String queryName : DELETE_CONTENT_QUERY_NAMES) {
dao.batchDeleteByNamedQuery(queryName, params);
}
}
When iteration try to perform EntityWithManyToMany.deleteByContentId HQL batch query, I can see on the logs the translated sql instruction, but followed by a set of insert of the same row and MySQL rightly returns me a constraint error.
This is my many to many relation object I try to delete:
@Entity
@Embeddable
public class CategoryContent implements java.io.Serializable {
/***
* Private Declarations
*/
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "contentId", column = @Column(
name = "CONTENT_ID",
nullable = false, precision = 10, scale = 0)),
@AttributeOverride(name = "categoryId", column = @Column(
name = "CATEGORY_ID",
nullable = false, precision = 10, scale = 0))
})
@NotNull
private CategoryContentId id;
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name = "CATEGORY_ID", insertable=false, updatable=false)
private Category category;
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name = "CONTENT_ID", insertable=false, updatable=false)
private Content content;
@Column(name = "PRIORITY", nullable = true)
private Integer priority;
}
Do you think it's possible the problem concern CascadeType? How can I solve this?
Any suggestions are appreciated!
Thanks a lot,
Davide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你走在正确的轨道上 - 尝试将你的级联修改为
这应该有效,因为你有一个连接表。
You're on the right track - try amending your cascade to
This should work because you have a Join table.