[英]Soft delete with @MappedSuperClass in Hibernate
I have an abstract base class A
, which is a @MappedSuperClass
. 我有一个抽象基类A
,它是一个@MappedSuperClass
。 Also, there are several entity classes which extend from class A
. 同样,有几个实体类从A
类扩展。 Class A
contains an attribute called STATUS
, which represents whether a record is deleted or not. 类A
包含一个名为STATUS
的属性,它表示是否删除一条记录。
@MappedSuperclass
public abstract class A {
@Id
private Long id;
@Column(nullable = false)
private boolean status = true;
}
What I want is to be able to perform a soft delete on all the child classes from class A
with @SQLDelete
annotation. 我想要的是能够使用@SQLDelete
批注对类A
所有子类执行软删除。 For example, I have a class B extends A
and whenever I call delete on class B
, I want it to update status of that record on database. 例如,我有一个B extends A
类B extends A
并且每当我对B
类调用delete时,我都希望它更新数据库上该记录的状态。
@Entity
@Table(name = "TempTable")
@SQLDelete(sql = "update TempTable set STATUS = 0 where ID = ?") //Basically, I don't want
//to write this in every
//class. Instead write
//it to class A once.
@Where(clause = "STATUS = 1")
public class B extends A {
private String alpha;
}
Right now I am able to soft delete, but in order to that I have to write @SQLDelete
annotation in every class. 现在,我可以进行软删除,但是为此,我必须在每个类中编写@SQLDelete
批注。 I don't want that repetition and want to write it in base class A
once. 我不想重复,只想在基类A
编写一次。 In there it will update status of that record. 在那里它将更新该记录的状态。
Is this possible? 这可能吗? If yes, how can I achieve it? 如果是,我该如何实现?
Thanks in advance. 提前致谢。
If you must use @SQLDelete then I'm afraid there is no solution. 如果必须使用@SQLDelete,那么恐怕没有解决方案。
I've managed to implement a soft-delete mechanism with spring-data-jpa + hibernate and mapped super classes without using @SQLDelete, but it requires creating your own BaseRepository implementation to override the SimpleJpaRepositoryImpl, and then replacing the delete method with your softdelete implementation. 我已经设法在不使用@SQLDelete的情况下使用spring-data-jpa + hibernate和映射的超类实现了软删除机制,但是它需要创建自己的BaseRepository实现以覆盖SimpleJpaRepositoryImpl,然后用您的softdelete替换delete方法实施。
The way to do this is well described in the spring-data docs, here . spring-data文档( 此处)中对此进行了很好的描述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.