Column Types

Kotlin type inference

Kronos chooses a column type from each persisted KPojo property when the property has no 📚Annotations. The selected database dialect renders the final DDL type when the table is created or synchronized.

import com.kotlinorm.annotations.Table
import com.kotlinorm.interfaces.KPojo
import java.time.LocalDateTime

@Table("tb_type_profile")
data class TypeProfile(
    var enabled: Boolean? = null,
    var score: Int? = null,
    var payload: ByteArray? = null,
    var createdAt: LocalDateTime? = null,
) : KPojo

For this model, Kronos selects the following column types:

PropertyInferred KColumnType
enabledBIT
scoreINT
payloadBLOB
createdAtDATETIME

MySQL renders those columns as:

`enabled` TINYINT(1)
`score` INT(11)
`payload` BLOB
`createdAt` DATETIME

Default mapping

Use this table as a compact reference for automatic mapping. The selected database dialect renders the final DDL type; see 📚Column Type Reference for dialect examples.

Kotlin typeInferred KColumnType
kotlin.BooleanBIT
kotlin.ByteTINYINT
kotlin.ShortSMALLINT
kotlin.IntINT
kotlin.LongBIGINT
kotlin.FloatFLOAT
kotlin.DoubleDOUBLE
java.math.BigDecimalDECIMAL
kotlin.CharCHAR
kotlin.StringVARCHAR
java.util.Date, java.sql.Date, java.time.LocalDate, kotlinx.datetime.LocalDateDATE
java.time.LocalTime, kotlinx.datetime.LocalTimeTIME
java.time.LocalDateTime, kotlinx.datetime.LocalDateTimeDATETIME
java.sql.TimestampTIMESTAMP
kotlin.time.Instant, kotlinx.datetime.InstantTIMESTAMP
kotlin.ByteArrayBLOB
Scalar enumVARCHAR
@Serialize propertyVARCHAR unless @ColumnType overrides it
Other property typesVARCHAR

Scalar enums use Enum.name for string-compatible columns. An integer @ColumnType stores the enum position. Use a custom mapping for a business code, and see 📚Enum Fields for examples.

Override with @ColumnType

Use @ColumnType to select the column type for one property.

import com.kotlinorm.annotations.ColumnType
import com.kotlinorm.annotations.Table
import com.kotlinorm.enums.KColumnType
import com.kotlinorm.interfaces.KPojo

@Table("tb_override_type")
data class OverrideType(
    @ColumnType(KColumnType.TEXT)
    var bio: String? = null,
    @ColumnType(KColumnType.UUID)
    var externalId: String? = null,
) : KPojo

For this model, the selected column types are:

PropertyKColumnType
bioTEXT
externalIdUUID

PostgreSQL renders them as:

"bio" TEXT
"externalId" UUID

Set length and scale

The @ColumnType annotation also accepts length and scale. String-like types use length; decimal types use length and scale.

import com.kotlinorm.annotations.ColumnType
import com.kotlinorm.annotations.Table
import com.kotlinorm.enums.KColumnType
import com.kotlinorm.interfaces.KPojo
import java.math.BigDecimal

@Table("tb_invoice_line")
data class InvoiceLine(
    @ColumnType(KColumnType.VARCHAR, length = 80)
    var title: String? = null,
    @ColumnType(KColumnType.DECIMAL, length = 12, scale = 2)
    var amount: BigDecimal? = null,
) : KPojo

DDL type fragments:

Dialecttitleamount
MySQLVARCHAR(80)DECIMAL(12,2)
PostgreSQLVARCHAR(80)DECIMAL(12,2)
SQLiteTEXTNUMERIC
H2VARCHAR(80)NUMERIC(12,2)
SQLServerVARCHAR(80)DECIMAL(12,2)
OracleVARCHAR2(80)NUMBER(12,2)
DM8VARCHAR2(80)NUMBER(12,2)

Use a native JSON column

With the Gson or Kotlinx Serialization setup shown on 📚Serialization, @Serialize properties are encoded as JSON text. Add @ColumnType with KColumnType.JSON when the database table should use a native JSON column.

import com.kotlinorm.annotations.ColumnType
import com.kotlinorm.annotations.Serialize
import com.kotlinorm.annotations.Table
import com.kotlinorm.enums.KColumnType
import com.kotlinorm.interfaces.KPojo

data class AuditPayload(val ip: String, val tags: List<String>)

@Table("tb_audit_event")
data class AuditEvent(
    @ColumnType(KColumnType.JSON)
    @Serialize
    var payload: AuditPayload? = null,
) : KPojo

When the registered JSON configuration saves AuditPayload("127.0.0.1", listOf("login")), the insert parameter is:

payload -> {"ip":"127.0.0.1","tags":["login"]}

DDL type fragments:

Dialectpayload
MySQLJSON
PostgreSQLJSONB
SQLiteTEXT
H2JSON
SQLServerJSON
OracleJSON
DM8JSON

The registered JSON configuration sends payload as a JSON string through JDBC. @ColumnType selects the type used in generated table DDL. Use the default VARCHAR column for JSON text tables.