Skip to content

Schema Sync

Schema Sync allows you to define database structures through entity classes. Kite automatically generates and maintains database tables, columns and indexes based on annotations.

Global Configuration

Configure Schema Sync behavior via SchemaConfig:

OptionDefaultDescription
enabledfalseWhether to enable Schema Sync
sqlLowercasetrueWhether generated SQL is lowercase
scanPackages[]Entity class scan packages, supports wildcards
createMissingTablestrueWhether to create missing tables
dropExistingTablesfalseWhether to drop existing tables
createMissingColumnstrueWhether to add missing columns
dropExistingColumnsfalseWhether to drop extra columns
createMissingIndexestrueWhether to create missing indexes
modifyIndexesfalseWhether to modify existing indexes
dropExistingIndexesfalseWhether to drop extra indexes

Enabling Example

java
import com.tang.kite.config.schema.SchemaConfig;

SchemaConfig.setEnabled(true);
SchemaConfig.getScanPackages().add("com.example.entity");

@Table Annotation

The @Table annotation maps entity classes to database tables. Schema Sync related attributes:

AttributeTypeDescription
valueStringTable name, defaults to class name
commentStringTable comment
java
import com.tang.kite.annotation.Table;

@Table(value = "account", comment = "账户表")
public class Account {}

@Column Annotation

The @Column annotation maps entity class fields to database columns. Schema Sync related attributes:

AttributeTypeDescription
valueStringColumn name, defaults to field name
ignoreBooleanWhether to ignore this column, omitting it from the database
dataTypeStringColumn data type, auto-detected if empty
lengthIntColumn length for VARCHAR, CHAR, etc., default 255
precisionIntNumeric precision for DECIMAL, NUMERIC, etc., default 10
scaleIntNumeric scale for DECIMAL, NUMERIC, etc., default 2
nullableBooleanWhether to allow NULL, default true
defaultValueArray<String>Column default value
commentArray<String>Column comment
foreignKeyStringReferenced foreign key table name
foreignKeyColumnStringReferenced column of the foreign key table
onDeleteForeignKeyActionForeign key action on delete
onUpdateForeignKeyActionForeign key action on update
java
import com.tang.kite.annotation.Column;
import com.tang.kite.sql.ast.ForeignKeyAction;

public class Account {

    @Column(value = "username", length = 50, comment = "用户名")
    private String username;

    @Column(defaultValue = "0", comment = "状态")
    private Integer status;

    @Column(foreignKey = "dept", foreignKeyColumn = "id", onDelete = ForeignKeyAction.CASCADE)
    private Long deptId;

}

Indexes

@Index Annotation

Creates indexes on individual fields.

AttributeTypeDescription
nameStringIndex name, auto-generated if empty
uniqueBooleanWhether the index is unique, default false
orderIndexOrderSort order, default ASC
java
import com.tang.kite.annotation.Index;
import com.tang.kite.enumeration.IndexOrder;

public class Account {

    @Index(unique = true)
    private String username;

    @Index(name = "idx_status", order = IndexOrder.DESC)
    private Integer status;

}

@CompositeIndex Annotation

Defines multi-column composite indexes at the class level.

AttributeTypeDescription
nameStringIndex name, auto-generated if empty
uniqueBooleanWhether the index is unique, default false
columnsArray<String>Column names in the composite index
ordersArray<IndexOrder>Sort order for each column
java
import com.tang.kite.annotation.CompositeIndex;

@CompositeIndex(columns = {"username", "status"}, unique = true)
@CompositeIndex(name = "idx_time", columns = {"createTime", "updateTime"})
public class Account {}

Released under the MIT License