ORM for Kotlin: Comparison of Exposed, Room, and Ktorm with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

ORM for Kotlin: Comparison and Examples

When developing applications in Kotlin, working with databases is one of the key tasks. Instead of writing raw SQL queries, developers increasingly use ORM (Object-Relational Mapping) — tools that automate the conversion of data between the object model and relational tables. In this article, we will look at three popular ORMs for Kotlin: Exposed, Room, and Ktorm. We will compare their architecture, performance, ease of use, and provide practical code examples.



1. Exposed — A Lightweight DSL Library from JetBrains

Exposed is a library developed by the creators of Kotlin. It is not a full-fledged ORM in the classical sense (like Hibernate), but rather provides two approaches: DSL (Domain Specific Language) for writing queries in Kotlin style and DAO (Data Access Objects) for working with objects. Exposed works on top of JDBC and supports PostgreSQL, MySQL, H2, SQLite, and others.



Code Example with Exposed (DSL)

// Database connectionDatabase.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")

// Table definitionobject Users : Table("users") { val id = integer("id").autoIncrement() val name = varchar("name", 50) val age = integer("age") override val primaryKey = PrimaryKey(id)}

// Insert and selecttransaction { SchemaUtils.create(Users) Users.insert { it[name] = "Alice" it[age] = 30 } Users.selectAll().forEach { println("${it[Users.name]}: ${it[Users.age]}") }}


Pros and Cons of Exposed

  • Pros: lightweight, full control over SQL, good integration with Kotlin Coroutines, active community.
  • Cons: no automatic schema generation (must be created manually), no built-in migration support (requires Flyway or Liquibase).


2. Room — The Official ORM for Android from Google

Room is a library that is part of Android Jetpack. It provides an abstraction layer over SQLite and automatically generates code for data access. Room uses annotations and compile-time processing (KAPT or KSP), which ensures high performance and type safety.



Code Example with Room

// Entity@Entity(tableName = "users")data class User(    @PrimaryKey(autoGenerate = true) val id: Int = 0,    val name: String,    val age: Int)

// DAO@Daointerface UserDao { @Query("SELECT * FROM users") suspend fun getAll(): List<User>

@Insert suspend fun insert(user: User)}

// Database@Database(entities = [User::class], version = 1)abstract class AppDatabase : RoomDatabase() { abstract fun userDao(): UserDao}


Pros and Cons of Room

  • Pros: full integration with Android, support for Flow and LiveData, automatic code generation, built-in migrations.
  • Cons: tied to the Android platform (not suitable for server or desktop applications), heavier compared to Exposed.


3. Ktorm — A Functional ORM for Kotlin

Ktorm is a modern ORM library that offers a functional style of working with databases. It does not use annotations or reflection, but relies on Kotlin DSL and extensions. Ktorm supports many DBMS (MySQL, PostgreSQL, Oracle, SQLite, etc.) and can work with both JDBC and SQL dialects.



Code Example with Ktorm

// Connection and table definitionval database = Database.connect("jdbc:mysql://localhost/mydb", user = "root", password = "pass")

object Users : Table<Nothing>("users") { val id = int("id").primaryKey() val name = varch

Recommendations