Add soft-delete module: @SoftDelete (DELETED/ACTIVE/TIMESTAMP) vs @SQLDelete + @SQLRestriction on Hibernate 7.4.14.Final, H2 and PostgreSQL 17
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b7rnC7jppGGdHGESvy
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
|
||||
/** Boolean strategy plus a business key (email). The unique-constraint tests add their indexes by hand. */
|
||||
@Entity
|
||||
@Table(name = "account")
|
||||
@SoftDelete
|
||||
public class Account {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String email;
|
||||
|
||||
protected Account() {}
|
||||
public Account(String email) { this.email = email; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getEmail() { return email; }
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.hibernate.annotations.SoftDeleteType;
|
||||
|
||||
/** Timestamp strategy plus a business key (email). */
|
||||
@Entity
|
||||
@Table(name = "account_stamped")
|
||||
@SoftDelete(strategy = SoftDeleteType.TIMESTAMP, columnName = "deleted_at")
|
||||
public class AccountStamped {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String email;
|
||||
|
||||
protected AccountStamped() {}
|
||||
public AccountStamped(String email) { this.email = email; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getEmail() { return email; }
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.hibernate.annotations.SoftDeleteType;
|
||||
|
||||
/** {@code SoftDeleteType.ACTIVE}: the column is {@code active} and the meaning is inverted (true = still there). */
|
||||
@Entity
|
||||
@Table(name = "article_active")
|
||||
@SoftDelete(strategy = SoftDeleteType.ACTIVE)
|
||||
public class ArticleActive {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String title;
|
||||
|
||||
protected ArticleActive() {}
|
||||
public ArticleActive(String title) { this.title = title; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getTitle() { return title; }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
|
||||
/**
|
||||
* The default strategy: {@code SoftDeleteType.DELETED}. Hibernate adds a {@code deleted boolean not null}
|
||||
* column (true = removed). The column is NOT a mapped attribute: there is no field for it below.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "article_deleted")
|
||||
@SoftDelete
|
||||
public class ArticleDeleted {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String title;
|
||||
|
||||
protected ArticleDeleted() {}
|
||||
public ArticleDeleted(String title) { this.title = title; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getTitle() { return title; }
|
||||
public void setTitle(String title) { this.title = title; }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
import org.hibernate.annotations.SQLRestriction;
|
||||
|
||||
/** The hand-rolled recipe that predates {@code @SoftDelete}: a mapped column, an UPDATE instead of DELETE, a WHERE on every read. */
|
||||
@Entity
|
||||
@Table(name = "article_manual")
|
||||
@SQLDelete(sql = "update article_manual set deleted = true where id = ?")
|
||||
@SQLRestriction("deleted = false")
|
||||
public class ArticleManual {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String title;
|
||||
@Column(nullable = false)
|
||||
private boolean deleted;
|
||||
|
||||
protected ArticleManual() {}
|
||||
public ArticleManual(String title) { this.title = title; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getTitle() { return title; }
|
||||
public boolean isDeleted() { return deleted; }
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/** Spring Data over the boolean-strategy entity. Every method is exercised by SpringDataTest. */
|
||||
public interface ArticleRepository extends JpaRepository<ArticleDeleted, Long> {
|
||||
|
||||
List<ArticleDeleted> findByTitle(String title);
|
||||
|
||||
long countByTitle(String title);
|
||||
|
||||
boolean existsByTitle(String title);
|
||||
|
||||
/** Derived delete: Spring Data loads the matching entities, then calls em.remove on each. Needs a transaction (plain Spring Data behaviour, not soft-delete specific). */
|
||||
@Transactional
|
||||
long deleteByTitle(String title);
|
||||
|
||||
/** HQL bulk delete: @SoftDelete turns it into an UPDATE. */
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query("delete from ArticleDeleted a where a.title = :title")
|
||||
int bulkDeleteByTitle(String title);
|
||||
|
||||
/** Native SQL: the soft-delete predicate is NOT added. */
|
||||
@Query(value = "select count(*) from article_deleted", nativeQuery = true)
|
||||
long countIncludingDeleted();
|
||||
|
||||
@Query(value = "select * from article_deleted where id = :id", nativeQuery = true)
|
||||
Optional<ArticleDeleted> findEvenIfDeleted(Long id);
|
||||
|
||||
/** The only way to un-delete: the flag is not an attribute, so HQL cannot name it. */
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query(value = "update article_deleted set deleted = false where id = :id", nativeQuery = true)
|
||||
int restore(Long id);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.hibernate.annotations.SoftDeleteType;
|
||||
|
||||
/** {@code SoftDeleteType.TIMESTAMP}: a nullable timestamp column; NULL = alive, a value = when it was removed. */
|
||||
@Entity
|
||||
@Table(name = "article_stamped")
|
||||
@SoftDelete(strategy = SoftDeleteType.TIMESTAMP, columnName = "deleted_at", comment = "NULL while the row is alive")
|
||||
public class ArticleStamped {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String title;
|
||||
|
||||
protected ArticleStamped() {}
|
||||
public ArticleStamped(String title) { this.title = title; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getTitle() { return title; }
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.hibernate.type.YesNoConverter;
|
||||
|
||||
/** Legacy-schema flavour: column {@code is_gone char(1)} holding 'Y' / 'N', via the {@code converter} attribute. */
|
||||
@Entity
|
||||
@Table(name = "article_yes_no")
|
||||
@SoftDelete(columnName = "is_gone", converter = YesNoConverter.class, comment = "Y = deleted")
|
||||
public class ArticleYesNo {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String title;
|
||||
|
||||
protected ArticleYesNo() {}
|
||||
public ArticleYesNo(String title) { this.title = title; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Parent side of a soft-deleted one-to-many. No cascade: deleting an author must not touch the books. */
|
||||
@Entity
|
||||
@Table(name = "author")
|
||||
@SoftDelete
|
||||
public class Author {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
@OneToMany(mappedBy = "author")
|
||||
private List<Book> books = new ArrayList<>();
|
||||
|
||||
protected Author() {}
|
||||
public Author(String name) { this.name = name; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getName() { return name; }
|
||||
public List<Book> getBooks() { return books; }
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/** Child of {@link Author} (the to-one is EAGER: Hibernate refuses LAZY towards a @SoftDelete entity, see A05b). Also, and owner of a plain many-to-many to {@link Tag}. */
|
||||
@Entity
|
||||
@Table(name = "book")
|
||||
@SoftDelete
|
||||
public class Book {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String title;
|
||||
@ManyToOne
|
||||
private Author author;
|
||||
@ManyToMany
|
||||
@JoinTable(name = "book_tag")
|
||||
private Set<Tag> tags = new HashSet<>();
|
||||
|
||||
protected Book() {}
|
||||
public Book(String title, Author author) { this.title = title; this.author = author; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getTitle() { return title; }
|
||||
public Author getAuthor() { return author; }
|
||||
public Set<Tag> getTags() { return tags; }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
|
||||
@Entity
|
||||
@Table(name = "item")
|
||||
@SoftDelete
|
||||
public class Item {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
@ManyToOne
|
||||
private Shelf shelf;
|
||||
|
||||
protected Item() {}
|
||||
public Item(String name) { this.name = name; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getName() { return name; }
|
||||
void setShelf(Shelf s) { this.shelf = s; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "note")
|
||||
public class Note extends SoftBase {
|
||||
private String text;
|
||||
|
||||
protected Note() {}
|
||||
public Note(String text) { this.text = text; }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
|
||||
/**
|
||||
* {@code @SoftDelete} on the COLLECTION: removing a tag from {@code subscriptions} soft-deletes the join-table row
|
||||
* ({@code reader_subscription.deleted}) instead of deleting it. The Reader and Tag entities stay as they are.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "reader")
|
||||
public class Reader {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
@ManyToMany
|
||||
@JoinTable(name = "reader_subscription")
|
||||
@SoftDelete
|
||||
private Set<Tag> subscriptions = new HashSet<>();
|
||||
|
||||
protected Reader() {}
|
||||
public Reader(String name) { this.name = name; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public Set<Tag> getSubscriptions() { return subscriptions; }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
|
||||
/** Parent with CascadeType.REMOVE and orphanRemoval: shows that cascades fire soft deletes on the children. */
|
||||
@Entity
|
||||
@Table(name = "shelf")
|
||||
@SoftDelete
|
||||
public class Shelf {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String label;
|
||||
@OneToMany(mappedBy = "shelf", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private List<Item> items = new ArrayList<>();
|
||||
|
||||
protected Shelf() {}
|
||||
public Shelf(String label) { this.label = label; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public List<Item> getItems() { return items; }
|
||||
public void add(Item i) { items.add(i); i.setShelf(this); }
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.hibernate.annotations.SoftDeleteType;
|
||||
|
||||
/** One place to say "soft delete" for a family of entities. */
|
||||
@MappedSuperclass
|
||||
@SoftDelete(strategy = SoftDeleteType.TIMESTAMP, columnName = "deleted_at")
|
||||
public abstract class SoftBase {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
public Long getId() { return id; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/** Only used by the Spring Data chapter ({@code SpringDataTest}); the other tests bootstrap Hibernate directly. */
|
||||
@SpringBootApplication
|
||||
public class SoftDeleteApplication {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.ankurm.softdelete;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
|
||||
@Entity
|
||||
@Table(name = "tag")
|
||||
@SoftDelete
|
||||
public class Tag {
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String label;
|
||||
|
||||
protected Tag() {}
|
||||
public Tag(String label) { this.label = label; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getLabel() { return label; }
|
||||
}
|
||||
Reference in New Issue
Block a user