Hibernate:用继承覆盖sql-delete

发布于 2024-10-25 13:03:38 字数 470 浏览 6 评论 0原文

我有一个实体 AB extends A 并尝试使用联合继承策略进行软删除。

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@SQLDelete("UPDATE A SET deleted = 1 WHERE id = ?")
A {

    @Id long id;
    boolean deleted;
}

@Entity
B extends A {}

看来 Hibernate 正确地将表 A 设置为 deleted = 1,但也删除了表 B 中的整个条目。当然,我想保留这个条目。

对此有什么想法吗?

我正在使用 Hibernate 3.5.5 和基于注释的实体定义。也尝试过 Hibernate 3.6.2。

I have an entity A and B extends A and try to have a soft-delete with joined inheritance strategy.

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@SQLDelete("UPDATE A SET deleted = 1 WHERE id = ?")
A {

    @Id long id;
    boolean deleted;
}

@Entity
B extends A {}

It seems that Hibernate properly sets the table A to deleted = 1, but also deletes the whole entry from table B. I would, of course, like to preserve this entry.

Any ideas on that?

I'm using Hibernate 3.5.5 and annotation-based entity definition. Tried Hibernate 3.6.2 as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

独夜无伴 2024-11-01 13:03:38

您想要创建一个DeleteEventListener,如下所示:

public class SoftDeleteEventListener extends DefaultDeleteEventListener {

private static final long serialVersionUID = 1L;

@Override
public void onDelete(DeleteEvent event, Set arg1) throws HibernateException {
    Object o = event.getObject();
    if (o instanceof SoftDeletable) {
        ((SoftDeletable)o).setStatusId(1);
        EntityPersister persister = event.getSession().getEntityPersister( event.getEntityName(), o);
        EntityEntry entityEntry = event.getSession().getPersistenceContext().getEntry(o);
        cascadeBeforeDelete(event.getSession(), persister, o, entityEntry, arg1);

        cascadeAfterDelete(event.getSession(), persister, o, arg1);

    } else {
        super.onDelete(event, arg1);
    }
}

}

将其挂接到您的 persistence.xml 中,如下所示

<property name = "hibernate.ejb.event.delete" value = "org.something.SoftDeleteEventListener"/> 

另外,不要忘记更新注释中的级联。

You'd want to create a DeleteEventListener as such:

public class SoftDeleteEventListener extends DefaultDeleteEventListener {

private static final long serialVersionUID = 1L;

@Override
public void onDelete(DeleteEvent event, Set arg1) throws HibernateException {
    Object o = event.getObject();
    if (o instanceof SoftDeletable) {
        ((SoftDeletable)o).setStatusId(1);
        EntityPersister persister = event.getSession().getEntityPersister( event.getEntityName(), o);
        EntityEntry entityEntry = event.getSession().getPersistenceContext().getEntry(o);
        cascadeBeforeDelete(event.getSession(), persister, o, entityEntry, arg1);

        cascadeAfterDelete(event.getSession(), persister, o, arg1);

    } else {
        super.onDelete(event, arg1);
    }
}

}

hook it into your persistence.xml like this

<property name = "hibernate.ejb.event.delete" value = "org.something.SoftDeleteEventListener"/> 

Also, don't forget to update your cascades in your annotations.

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