31 Ağustos 2021 Salı

JPA Soft Delete

Giriş
JPA Soft Delete için bir anotasyon sağlamıyor. 

Hibernate Çözümü
@SQLDelete + @Where kullanılır. Açıklaması şöyle
@SQLDelete takes a native SQL query that is executed whenever a Hibernate-managed entity gets deleted.
@Where takes a condition and appends it to 'select' queries automatically, allowing you to filter entities based on the deleted attribute automatically.
Örnek
Şöyle yaparız
@Entity
@Table(name = "article")
@SQLDelete(sql = "update article set deleted=true where id=?")
@Where(clause = "deleted = false")
public class Article {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE)
  @Column(name = "id", nullable = false)
  private Long id;

  @Column(name = "deleted", nullable = false)
  private Boolean deleted = false;

  // other properties, getters and setters omitted
}
Örnek
Şöyle yaparız
@Entity
@SQLDelete(sql = "UPDATE invoice SET status_record = 'INACTIVE' WHERE id=?")
@Where(clause = "status_record = 'ACTIVE'")
public class Invoice extends BaseEntity {
  ...
}


Hiç yorum yok:

Yorum Gönder