Logical delete at a common place in hibernate

一个人想着一个人 提交于 2019-12-07 06:08:37

Try to replace the code in setIsActive method with:

public void setIsActive(Boolean isActive) {
    this.isActive = isActive;
}

in your code the use of variable name without this could be ambiguos...

I think you should also add @MappedSuperclass annotation to your abstract class to achieve field inheritance.

The issue with the proposed solution (which you allude to in your comment to that answer) is that does not handle cascading delete.

An alternative (Hibernate specific, non-JPA) solution might be to use Hibernate's @SQLDelete annotation:

http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/querysql.html#querysql-cud

I seem to recall however that this Annotation cannot be defined on the Superclass and must be defined on each Entity class.

The problem with Logical delete in general however is that you then have to remember to filter every single query and every single collection mapping to exclude these records.

In my opinion an even better solution is to forget about logical delete altogether. Use Hibernate Envers as an auditing mechanism. You can then recover any deleted records as required.

http://envers.jboss.org/

You can use the SQLDelete annotation...

@org.hibernate.annotations.SQLDelete;

//Package name...

//Imports...

@Entity
@Table(name = "CUSTOMER")
//Override the default Hibernation delete and set the deleted flag rather than deleting the record from the db.
@SQLDelete(sql="UPDATE customer SET deleted = '1' WHERE id = ?")
//Filter added to retrieve only records that have not been soft deleted.
@Where(clause="deleted <> '1'")
public class Customer implements java.io.Serializable {
private long id;
...
private char deleted;

Source: http://featurenotbug.com/2009/07/soft-deletes-using-hibernate-annotations/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!