Introduction
Exposed is an open-source SQL framework developed by JetBrains for Kotlin. It provides two complementary access layers: a typesafe DSL that mirrors SQL syntax in Kotlin code, and a DAO layer for lightweight ORM-style entity management. Both approaches leverage Kotlin's type system to catch query errors at compile time.
What Exposed Does
- Provides a typesafe DSL where table columns, joins, and conditions are Kotlin expressions
- Offers a DAO layer mapping database rows to Kotlin objects with lazy loading
- Manages database transactions with automatic commit, rollback, and retry
- Supports schema generation and migration through
SchemaUtils - Works with PostgreSQL, MySQL, MariaDB, SQLite, H2, Oracle, and SQL Server
Architecture Overview
Exposed defines tables as Kotlin objects extending Table. Columns are properties with types that enforce compile-time safety. The DSL layer translates Kotlin expressions into SQL strings via a dialect abstraction, while the DAO layer wraps rows in Entity objects backed by an identity map within the transaction scope. Connection pooling integrates with HikariCP or any JDBC DataSource.
Installation & Configuration
- Add
exposed-core,exposed-dao, andexposed-jdbcmodules via Gradle or Maven - Configure a
Database.connect()call with JDBC URL, driver, and credentials - Optionally add
exposed-java-timeorexposed-kotlin-datetimefor date column support - Use
exposed-spring-boot-starterfor automatic Spring Boot integration - Set
TransactionManager.defaultDatabasefor implicit transaction binding
Key Features
- Compile-time SQL validation eliminates entire classes of runtime query errors
- DSL queries compose naturally with Kotlin's
let,apply, and extension functions - DAO entities support one-to-many and many-to-many relations via
referencedOnandvia - Batch insert and upsert operations for efficient bulk data loading
- Coroutine-friendly with
newSuspendedTransactionfor non-blocking database access
Comparison with Similar Tools
- Hibernate/JPA — annotation-heavy ORM with XML config; Exposed uses Kotlin DSL for lighter, more idiomatic code
- jOOQ — Java-first SQL builder with code generation; Exposed is Kotlin-native and needs no code generation step
- Ktorm — similar Kotlin ORM; Exposed has broader database support and is maintained by JetBrains
- SQLDelight — generates Kotlin from SQL files; Exposed lets you write queries in Kotlin directly
- Spring Data JPA — repository-based abstraction; Exposed gives finer SQL control with less magic
FAQ
Q: Can Exposed handle database migrations?
A: SchemaUtils.createMissingTablesAndColumns() handles simple schema evolution. For production migrations, pair it with Flyway or Liquibase.
Q: Does Exposed support connection pooling?
A: Yes. Pass a HikariCP DataSource to Database.connect() for production-grade connection management.
Q: Is Exposed suitable for large-scale applications? A: Yes. JetBrains uses it in their own products. The DSL layer generates efficient SQL, and batch operations handle high-throughput scenarios.
Q: Can I use raw SQL with Exposed?
A: Yes. exec() and TransactionManager.current().exec() let you run arbitrary SQL when the DSL does not cover a specific need.