Hibernate多对多删除

发布于 2024-12-13 10:25:39 字数 2041 浏览 1 评论 0原文

当内容与另一个对象存在多对多关系时,我在删除内容时遇到一些麻烦。

为了执行删除,我有一个命名查询数组,要删除,我循环遍历该数组并执行每个实体的删除,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫漫岁月 2024-12-20 10:25:39

你走在正确的轨道上 - 尝试将你的级联修改为

cascade = {CascadeType.PERSIST, CascadeType.DELETE}

这应该有效,因为你有一个连接表。

You're on the right track - try amending your cascade to

cascade = {CascadeType.PERSIST, CascadeType.DELETE}

This should work because you have a Join table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文