Kotlin support

Doma supports Kotlin 1.4.0 or later.

Best practices

Here are some recommended methods, such as defining classes and building them with Kotlin.

Entity classes

  • Define as a plain class

  • Specify a Metamodel annotation to the metamodel element of @Entity

@Entity(metamodel = Metamodel())
class Person : AbstractPerson() {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = -1

    var name: Name? = null

    var age: Int? = -1

    var address: Address? = null

    @Column(name = "DEPARTMENT_ID")
    var departmentId: Int = -1

    var version: Int = -1

Domain classes

  • Define as a data class

  • Define only one constructor

  • Define only one property whose name is value in the constructor

  • Use val for the property definition

@Domain(valueType = String::class)
data class Name(val value: String)

Embeddable classes

  • Define as a data class

  • Define only one constructor

  • Define properties only in the constructor

  • Use val for the property definitions

data class Address(val city: String, val street: String)

Dao interfaces

  • Specify a SQL template to @org.seasar.doma.Sql

interface PersonDao {
  select * from person where id = /*id*/0
  fun selectById(id: Int): Person

  fun insert(person: Person): Int
val dao: PersonDao = ...
val person = Person(name = Name("John"), address = Address(city = "Tokyo", street = "Yaesu"))
val count = dao.insert(person)

Kotlin specific Criteria API


Prefer the Kotlin specific Criteria API to DAO interfaces.

Doma provides Kotlin specific Criteria API, KEntityql and KNativeSql DSLs. They are very similar with the Entityql and NativeSql DSLs, which are described in Criteria API. The biggest feature of the KEntityql and KNativeSql DSLs is simplicity.

For example, when you use KEntityql, you have to accept a lambda parameter in a WHERE expression as follows:

val entityql = Entityql(config)
val e = Employee_()

val list = entityql
    .where { c ->
        c.eq(e.departmentId, 2)
        c.or {
            c.gt(e.salary, Salary("1000"))
            c.lt(e.salary, Salary("2000"))

The lambda parameter c is a bit annoying. On the other hand, when you use KEntityql, the parameter is gone.

val entityql = KEntityql(config)
val e = Employee_()

val list = entityql
    .where {
        eq(e.departmentId, 2)
        or {
            gt(e.salary, Salary("1000"))
            lt(e.salary, Salary("2000"))

You can see a lot of sample code here.

The KEntityql and KNativeSql DSLs are included in doma-kotlin.jar. Note that you should depend on doma-kotlin instead of doma-core in your build script. You can write build.gradle.kts as follows:

dependencies {

Code Generation

Use Doma CodeGen Plugin. This plugin support Kotlin code generation.

Using kapt in Gradle

Annotation processors are supported in Kotlin with the kapt compiler plugin.

Add the dependencies using the kapt and implementation configuration in your dependencies block. For example, you can write build.gradle.kts as follows:

dependencies {

To simplify your build script, we recommend you use the Doma Compile Plugin:

Sample project