Skip to content

Logical Deletion

Logical deletion is a data processing method where data is not physically removed from the database when deleted, but marked as deleted. This approach preserves data integrity and traceability, while also allowing for data recovery when needed.

Logical Deletion Configuration

Kite provides configuration options for logical deletion, which can be set in LogicalDeletionConfig.

java
import com.tang.kite.config.logical.LogicalDeletionConfig;

// Enable logical deletion
LogicalDeletionConfig.enabled = true;

// Set logical deletion field name
LogicalDeletionConfig.fieldName = "deleted";

// Custom logical deletion processor
LogicalDeletionConfig.logicalDeletionProcessor = new CustomLogicalDeletionProcessor();

Logical Deletion Annotation

Kite provides the @LogicalDeletion annotation for specifying logical deletion fields in entity classes. This annotation takes precedence over the globally configured fieldName.

Usage Example

java
import com.tang.kite.annotation.logical.LogicalDeletion;

public class Account {

    private Long id;
    private String name;
    @LogicalDeletion
    private String deleted;

}

Logical Deletion Processor

Method List

Method NameDescriptionReturn Value
isTableNeedProcessing(tableClass: Class<*>)Determines if a specific table class needs processing. Default returns true, processing all tables.Boolean
process(field: Field)Processes the specified database field to generate logical deletion values.LogicalDeletionValue

Default Implementation

Kite provides a default logical deletion processor DefaultLogicalDeletionProcessor that supports generating logical deletion values for multiple data types:

Data TypeNormal ValueDeleted Value
Booleanfalsetrue
Byte01
Short01
Int01
Long0L1L
Char'0''1'
String"0""1"

Custom Processor

You can implement the LogicalDeletionProcessor interface to create a custom logical deletion processor:

java
import com.tang.kite.config.logical.LogicalDeletionProcessor;
import com.tang.kite.config.logical.LogicalDeletionValue;
import java.lang.reflect.Field;

public class CustomLogicalDeletionProcessor implements LogicalDeletionProcessor {

    @Override
    public boolean isTableNeedProcessing(Class<?> tableClass) {
        return tableClass == Account.class;
    }

    @Override
    public LogicalDeletionValue process(Field field) {
        return new LogicalDeletionValue("N", "Y");
    }

}

Configure Custom Processor

java
LogicalDeletionConfig.logicalDeletionProcessor = CustomLogicalDeletionProcessor();

Logical Deletion Manager

Kite provides the LogicalDeletionManager class for skipping logical deletion or temporarily using logical deletion in specific scenarios.

Method List

Method NameDescriptionParametersReturn Value
withLogical(block: Runnable)Temporarily uses logical deletion to execute a code blockblock: Runnable The code block to executeNone
withLogical<T>(block: Supplier<T>): TTemporarily uses logical deletion to execute a code block and return the resultblock: Supplier<T> The code block to executeT The return value of the code block
withSkip(block: Runnable)Skips logical deletion to execute a code blockblock: Runnable The code block to executeNone
withSkip<T>(block: Supplier<T>): TSkips logical deletion to execute a code block and return the resultblock: Supplier<T> The code block to executeT The return value of the code block

Usage Examples

Temporarily Using Logical Deletion

java
import com.tang.kite.logical.LogicalDeletionManager;

LogicalDeletionManager.withLogical(() -> {
    accountMapper.deleteById(1L);
});

Skipping Logical Deletion

java
import com.tang.kite.logical.LogicalDeletionManager;

LogicalDeletionManager.withSkip(() -> {
    accountMapper.deleteById(1L);
});

Released under the MIT License