English | 简体中文

api-docs / org.ktorm.dsl / insertAndGenerateKey

insertAndGenerateKey

fun <T : BaseTable<*>> Database.insertAndGenerateKey(
    table: T,
    block: AssignmentsBuilder.(T) -> Unit
): Any
(source code)

Construct an insert expression in the given closure, then execute it and return the auto-generated key.

This function assumes that at least one auto-generated key will be returned, and that the first key in
the result set will be the primary key for the row.

Usage:

val id = database.insertAndGenerateKey(Employees) {
set(it.name, "jerry")
set(it.job, "trainee")
set(it.managerId, 1)
set(it.hireDate, LocalDate.now())
set(it.salary, 50)
set(it.departmentId, 1)
}

Parameters

table - the table to be inserted.

block - the DSL block, an extension function of AssignmentsBuilder, used to construct the expression.

Since
2.7

Return
the first auto-generated key.