このコードは、Kotlin-Exposed Wikiから直接引っ張ってきましたが、動作しません。RDBMSを使用する必要があるクールなプロジェクトのアイデアがあるので、それを機能させることができないのは奇妙でイライラします。私は何が欠けていますか?壊れていますか?
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.selectAll
fun main(args: Array<String>) {
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
transaction {
logger.addLogger(StdOutSqlLogger)
val stPeteId = Cities.insert {
it[name] = "St. Petersburg"
} get Cities.id
println("Cities: ${Cities.selectAll()}")
}
}
// Table definition
object Cities : Table() {
val id = integer("id").autoIncrement().primaryKey()
val name = varchar("name", 50)
}
// Entity definition
data class City(
val id: Int,
val name: String
)
Intellij で実行すると、次のエラー メッセージが表示されます。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Table "CITIES" not found; SQL statement:
INSERT INTO CITIES (NAME) VALUES (?) [42102-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.readTableOrView(Parser.java:5552)
at org.h2.command.Parser.readTableOrView(Parser.java:5529)
at org.h2.command.Parser.parseInsert(Parser.java:1062)
at org.h2.command.Parser.parsePrepared(Parser.java:417)
at org.h2.command.Parser.parse(Parser.java:321)
at org.h2.command.Parser.parse(Parser.java:293)
at org.h2.command.Parser.prepareCommand(Parser.java:258)
at org.h2.engine.Session.prepareLocal(Session.java:578)
at org.h2.engine.Session.prepareCommand(Session.java:519)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:73)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:288)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:1188)
at org.jetbrains.exposed.sql.statements.InsertStatement.prepared(InsertStatement.kt:58)
at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:46)
at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:103)
at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:97)
at org.jetbrains.exposed.sql.statements.Statement.execute(Statement.kt:27)
at org.jetbrains.exposed.sql.QueriesKt.insert(Queries.kt:43)
at MainKt$main$1.invoke(Main.kt:14)
at MainKt$main$1.invoke(Main.kt)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:92)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:64)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:55)
at MainKt.main(Main.kt:11)
Process finished with exit code 1