Version v1.6 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot. For up-to-date documentation, see the latest version.

Query Builders


A function that builds part of a query is called a builder.

All builder functions are defined in org.komapper.core.dsl.query.


The where function builds a Where declaration.

val salaryWhere = where {
  e.salary greater BigDecimal(1_000)
val query: Query<List<Employee>> = QueryDsl.from(e).where(salaryWhere)


The on function builds a On declaration.

val departmentIdOn = on {
    e.departmentId eq d.departmentId
val query: Query<List<Employee>> = QueryDsl.from(e).innerJoin(d, departmentIdOn)


The having function builds a Having declaration.

val countHaving = having {
    count() greater 3
val query: Query<List<Int?>> = QueryDsl.from(e)


The set function builds an Assignment declaration.

val addressAssignment = set(a) {
    a.street eq "STREET 16"
val query: Query<Int> = QueryDsl.update(a).set(addressAssignment).where {
    a.addressId eq 1


The values function builds an Assignment declaration.

val addressAssignment = values(a) {
    a.street eq "STREET 16"
val query: Query<Pair<Int, Int?>> = QueryDsl.insert(a).values(addressAssignment)


The join function builds a Join element.

The Join element can be passed to the innerJoin or leftJoin functions:

val departmentJoin = join(d) {
    e.departmentId eq d.departmentId

val query1: Query<List<Employee>> = QueryDsl.from(e).innerJoin(departmentJoin)
val query2: Query<List<Employee>> = QueryDsl.from(e).leftJoin(departmentJoin)


The groupBy function builds a list of column expressions.

val groupByDepartmentId = groupBy(e.departmentId)
val query: Query<List<Int?>> = QueryDsl.from(e)
    .having {
        count() greater 3


The orderBy function builds a list of sort expressions.

val orderBySalaryAndNo = orderBy(e.salary, e.employeeNo)
val query: Query<List<Employee>> = QueryDsl.from(e).orderBy(orderBySalaryAndNo)
Last modified March 11, 2022: Update the title and linkTitle (cf9ed4f)