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
StarWarsFilmsTableis anIntIdTable, theStarWarsFilmsEntityclass extends fromIntEntity, which indicates that theidand primary key ofStarWarsFilmsTableis of typeInt.The
companion objectblock defines anEntityClasswhich is responsible for maintaining the relation between theStarWarsFilmsEntityclass and the actual table object,StarWarsFilmsTable.Each column in the table is represented as a property in the class, where the
bykeyword 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.