java – Hibernate:用inheritace覆盖sql-delete

我有一个实体A和B扩展A并尝试使用连接继承策略进行软删除.@Entity@Inheritance(strategy = InheritanceType.JOINED)@SQLDelete(UPDATE A SET deleted = 1 WHERE id = ?)A {@Id long id;boolean deleted;}@Enti...

我有一个实体A和B扩展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.

解决方法:

您想要创建一个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"/> 

另外,不要忘记在注释中更新您的级联.

本文标题为:java – Hibernate:用inheritace覆盖sql-delete

基础教程推荐