Hibernate在超类中定义@Where批注

时间:2018-07-16 17:17:17

标签: java hibernate spring-data

我正在尝试在我的spring-hibernate项目中实现软删除。 我的计划是在查询中使用@SQLDelete注释覆盖delete方法,并使用休眠@Where注释过滤逻辑删除的实体。

当我尝试在超类中定义@Where子句时,我遇到了一些困难。看来这些实体没有从抽象基类继承@Where子句。

注意:如果我将@Where注释移至实体类,一切将按预期工作

基础实体类:

@MappedSuperclass
@Where(clause = " IS_DELETED = false")
public abstract class BaseEntity {

   @Column(name = "IS_DELETED")
   private boolean isDeleted;

   public BaseEntity() {
   }

   public boolean getIsDeleted() {
      return this.isDeleted;
   }

   public void setIsDeleted(boolean isDeleted) {
      this.isDeleted = isDeleted;
   }
 }

实体类:

@Entity
@Table(name = "Events")
@SQLDelete(sql ="UPDATE events " +
                "SET IS_DELETED = true " +
                "WHERE id = ?")
public class Event extends BaseEntity {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "ID")
   private Long id;

   @Column(name = "NAME")
   private String name;

   public Event() {
   }

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

尝试与@Inherited一起添加@where注释。