English | 简体中文

api-docs / org.ktorm.schema / BaseTable / <init>

<init>

BaseTable(
    tableName: String,
    alias: String? = null,
    catalog: String? = null,
    schema: String? = null,
    entityClass: KClass<E>? = null)

Base class of Ktorm’s table objects, represents relational tables in the database.

This class provides the basic ability of table and column definition but doesn’t support any binding mechanisms.
If you need the binding support to Entity interfaces, use Table instead.

There is an abstract function doCreateEntity. Subclasses should implement this function, creating an entity object
from the result set returned by a query, using the binding rules defined by themselves. Here, the type of the entity
object could be an interface extending from Entity, or a data class, POJO, or any kind of classes.

Here is an example defining an entity as data class. The full documentation can be found at:
https://www.ktorm.org/en/define-entities-as-any-kind-of-classes.html

data class Staff(
val id: Int,
val name: String,
val job: String,
val hireDate: LocalDate
)
object Staffs : BaseTable<Staff>("t_employee") {
val id = int("id").primaryKey()
val name = varchar("name")
val job = varchar("job")
val hireDate = date("hire_date")
override fun doCreateEntity(row: QueryRowSet, withReferences: Boolean) = Staff(
id = row[id] ?: 0,
name = row[name].orEmpty(),
job = row[job].orEmpty(),
hireDate = row[hireDate] ?: LocalDate.now()
)
}

Since
2.5