当前位置:网站首页>数据的软删除—什么时候需要?又如何去实现?
数据的软删除—什么时候需要?又如何去实现?
2022-06-10 17:43:00 【InfoQ】
0. 阅读完本文你将会学会
- 什么是软删除?
- 如何考量是否使用软删除
- 如何在Spring里实现软删除
1. 前言
2. 什么是软删除(Soft Delete)?
2.1 软删除的概念
2.2 软删除的实现方式
- 添加布尔类型的字段
is_deletedis_activeis_archived- 添加时间戳字段
deleted_at- 将软删除的数据插入到另一个表中。
orderorder_deletedorderorder_deleted2.3 是否采用软删除的考量
- 订单不是被删除的,而是被“取消”的,订单取消得太晚,还会产生花费;
- 员工不是被删除的,而是被“解雇”的或者“退休”的。还有相应的补偿金要处理;
- 职位不是被删除的,是被“填补”的(或者招聘申请被驳回)。
3. 在Spring中实现软删除
3.1 实体类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 如何获取已被删除的数据
@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. 结语
边栏推荐
- 【ceph】ceph配置源码分析|common/config.*
- Abbexa AML1 DNA 结合 ELISA 试剂盒说明书
- 苹果放大招!这件事干的太漂亮了……
- LeetCode 255. 验证前序遍历序列二叉搜索树*
- Set up an online help center to easily help customers solve problems
- Abbexa CDAN1 siRNA使用说明书
- Canvas大火燃烧h5动画js特效
- Container containing the most water
- NaturalSpeech模型合成语音在CMOS测试中首次达到真人语音水平
- Postman interface test tool
猜你喜欢

Abbexa 无细胞 DNA 试剂盒说明书

Library for adding progress bar during training --tqdm

Wireshark学习笔记(一)常用功能案例和技巧

Faster Planner——Kinodynamic Astar详解

堆利用之chunk extend: HITCON tranining lab13

How to locate the hot problem of the game

Chunk extend: hit training lab13

使用Canvas实现的噪音线条h5js特效

红色垂直左侧边菜单导航代码

Linear mobile chess
随机推荐
XML & XPath parsing
分享我做Dotnet9博客网站时积累的一些资料
c语言---14 循环语句for
红色垂直左侧边菜单导航代码
c语言---13 循环语句while
阅读micropyton源码-添加C扩展类模块(1)
Some views on the current CIM (bim+gis) industry
QtMqtt 源码编译设置KeepAlive后ping包超时错误不返回问题修复(QMQTT::MqttNoPingResponse,QMQTT::ClientPrivate::onPingTimeo)
踩坑了,BigDecimal 使用不当,造成P0事故!
Cdga| six key points of data governance for industrial enterprises
The relationship between trees, forests and binary trees
内存池原理一(基于整块)
c语言---12 分支语句switch
js模糊阴影跟随动画js特效插件
Aesthetic experience (episode 237) Luo Guozheng
ZigBee模块无线传输星形拓扑组网结构简介
用脚本添加URP的RendererData
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
Mmdetection build_ Optimizer module interpretation
YML file configuration parameter definition dictionary and list