Entity definition
Representing database tables as Kotlin objects ensures type safety and allows you to work with database records just like regular Kotlin objects, taking full advantage of Kotlin's language features.
When using the DAO approach, IdTable
needs to be associated with an Entity
, because every database record in this table needs to be mapped to an Entity
instance, identified by its primary key.
An entity instance is defined as a class. In the following example, StarWarsFilmEntity
is the entity class linked to the table StarWarsFilmsTable
:
Since
StarWarsFilmsTable
is anIntIdTable
, theStarWarsFilmsEntity
class extends fromIntEntity
, which indicates that theid
and primary key ofStarWarsFilmsTable
is of typeInt
.The
companion object
block defines anEntityClass
which is responsible for maintaining the relation between theStarWarsFilmsEntity
class and the actual table object,StarWarsFilmsTable
.Each column in the table is represented as a property in the class, where the
by
keyword ensures the data is fetched or updated from the corresponding column when accessed.
Once the entity class is defined, instances of this class allow you to manipulate individual records from the corresponding table. This could involve creating a new record, retrieving a row based on its primary key, updating values, or deleting records.