Table
In Exposed, the Table class is the core abstraction for defining database tables. This class provides methods to define various column types, constraints, and other table-specific properties.
Table is located in the org.jetbrains.exposed.v1.core package of the exposed-core module.
The following example defines a table with an auto-incrementing integer id column and string name and email column:
object Customer : val id val name val email }Insert
Exposed provides several functions to insert rows into a table:
Insert a single row
To create a new table row, use the .insert() function:
Customer.insert it[name] it[email] }
}The .insert() function returns a InsertStatement object that contains the inserted row’s primary key value.
val id it[name] it[email] } get Customer.id // or }.get(Customer.id)If the same row already exists in the table, it throws an exception.
Batch insert
.batchInsert() allows mapping a list of entities into table rows in a single SQL statement.
It is more efficient than using the insert query for each row as it initiates only one statement.
suspendTransaction val customers ,
)
Customer. this[Customer.name] this[Customer.email] }
}Read
Retrieve all records
To retrieve all records from a table, use the .selectAll() method:
Query inherits Iterable so it is possible to traverse it using .map() or .forEach():
suspendTransaction
val customers ,
,
)
Customer. this[Customer.name] this[Customer.email] }
}
val result result shouldBe
}Retrieve a record
The .select() function allows you to select specific columns or/and expressions.
suspendTransaction val result result.map shouldBe
}