当前位置:网站首页>Soft deletion of data - when? How to realize it?
Soft deletion of data - when? How to realize it?
2022-06-10 19:52:00 【InfoQ】
0. After reading this article, you will learn
- What is soft deletion ?
- How to consider whether to use soft deletion
- How to be in Spring Soft deletion in
1. Preface
2. What is soft deletion (Soft Delete)?
2.1 The concept of soft deletion
2.2 Implementation of soft deletion
- Add a Boolean field
is_deletedis_activeis_archived- Add timestamp field
deleted_at- Insert soft deleted data into another table .
orderorder_deletedorderorder_deleted2.3 Whether to use soft deletion
- The order is not deleted , But be “ Cancel ” Of , The order was cancelled too late , There will also be costs ;
- Employees are not deleted , But be “ fire ” Or “ retired ” Of . There are also corresponding compensation to deal with ;
- The position is not deleted , Be being “ fill ” Of ( Or the recruitment application is rejected ).
3. stay Spring Soft deletion in
3.1 Entity class Product
package com.jayxu.mydemo.persistence.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "product")
public class Product {
private long id;
private String name;
private double price;
private String description;
private boolean isDeleted = Boolean.FALSE;
// getter setter methods
}
isDeleted@Entity
@Table(name = "product")
@SQLDelete(sql = "UPDATE product SET is_deleted = true WHERE id = ?")
@Where(clause = "is_deleted = false")
public class Product {
// . . .
}
@SQLDeleteisDeleted@whereProductis_deleted = true3.2 Repository
package com.jayxu.mydemo.repository;
import com.jayxu.mydemo.persistence.entity.Product;
import org.springframework.data.repository.CrudRepository;
public interface ProductRepository extends CrudRepository<Product, Long> {
}
3.3 Service
package com.jayxu.mydemo.service;
import com.jayxu.mydemo.persistence.entity.Product;
import com.jayxu.mydemo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@Autowired
ProductRepository productRepository;
public Product create(Product product){
return productRepository.save(product);
}
public void deleteById(long id){
productRepository.deleteById(id);
}
public Iterable<Product> findAll(){
return productRepository.findAll();
}
}
3.4 How to get deleted data
@Where@FilterDef@Filterpackage com.jayxu.mydemo.persistence.entity;
import org.hibernate.annotations.*;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "product")
@SQLDelete(sql = "UPDATE product SET is_deleted = true WHERE id = ?")
@FilterDef(name = "removedProductFilter"
, parameters = @ParamDef(name = "isDeleted", type = "boolean"))
@Filter(name = "removedProductFilter", condition = "is_deleted = :isDeleted")
public class Product {
// . . .
}
@FilterDef @Filter@FilterProductServicefindAll() package com.jayxu.mydemo.service;
import com.jayxu.mydemo.persistence.entity.Product;
import com.jayxu.mydemo.repository.ProductRepository;
import org.hibernate.Filter;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
@Service
public class ProductService {
@Autowired
ProductRepository productRepository;
@Autowired
EntityManager entityManager;
private String FILTER_REMOVED_PRODUCT = "removedProductFilter";
private String PARAM_IS_DELETED = "isDeleted";
public Product create(Product product){
return productRepository.save(product);
}
public void deleteById(long id){
productRepository.deleteById(id);
}
public Iterable<Product> findAll(boolean isDeleted){
Session session = entityManager.unwrap(Session.class);
Filter removedProductFilter = session.enableFilter(FILTER_REMOVED_PRODUCT);
removedProductFilter.setParameter(PARAM_IS_DELETED, isDeleted);
Iterable<Product> products = productRepository.findAll();
session.disableFilter(FILTER_REMOVED_PRODUCT);
return products;
}
}
session.enableFilter()removedProductFiltersession.disableFilter()removedProductFilterProductRepositoryfindAllByIsDeleted()4. Conclusion
边栏推荐
- Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close registration | geek headlines
- 618 great promotion is coming, mining bad reviews with AI and realizing emotional analysis of 100 million comments with zero code
- 禁止摆烂,软件测试工程师从初级到高级进阶指南,助你一路晋升
- How to query the database table storage corresponding to a field on the sapgui screen
- 大厂是怎么写数据分析报告的?
- SAR回波信号基本模型与性质
- [advanced C language] advanced pointer [Part 2]
- Cet article vous donne un aperçu de la tâche future de j.u.c, du cadre Fork / join et de la file d'attente de blocage
- 【C语言进阶】指针的进阶【中篇】
- 一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
猜你喜欢

大厂是怎么写数据分析报告的?

Mysql database design concept (multi table query & transaction operation)

Basic model and properties of SAR echo signal

Which school do you choose after the college entrance examination? VR panoramic campus all-round display

Super simple course design SSM student management system (including simple addition, deletion, modification and query of source code)

Summary of "performance test" of special test

MicroNet实战:使用MicroNet实现图像分类

Spark ShuffleManager

When the college entrance examination is opened, VR panorama can see the test site in this way

【C语言】一不小心写出bug?凡人教你如何写出好代码【详解vs中调试技巧】
随机推荐
Morris traversal of binary tree
腾讯Libco协程开源库 源码分析 全系列总结博客
100003 words, take you to decrypt the system architecture under the double 11 and 618 e-commerce promotion scenarios
叮咚抢菜-派送时段监听及推送工具
Harbor image pull voucher configuration
Zabbix Server Trapper远程代码执行漏洞(CVE-2017-2824)
2022.05.26 (lc_1143_longest common subsequence)
Ranked first in China's SDN (software) market share for six consecutive years
Looking for a room in the graduation season of college students, VR panoramic viewing helps you screen Online
【C语言进阶】指针的进阶【中篇】
The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?
Cet article vous donne un aperçu de la tâche future de j.u.c, du cadre Fork / join et de la file d'attente de blocage
DDD landing practice repeat record of theoretical training & Event storm
Does the giraffe's neck grow longer not because it eats leaves from high places? Scientists have found the answer in fossils 17million years ago
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
SAR回波信号基本模型与性质
Tencent libco collaboration open source library source code analysis full series summary blog
中国 璞富腾酒店及度假村旗下酒店推出全新水疗产品共庆6月11日全球健康日
Deep understanding of lightgbm
How to query the database table storage corresponding to a field on the sapgui screen