1

此代码直接从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
4

1 回答 1

5

我认为他们的示例中缺少 create 语句。该项目的 GitHub 页面上的 DAO 示例似乎有一些您指向的示例没有的语句。

尝试添加一个 create 语句:

fun main(args: Array<String>) {
    Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")

    transaction {
        logger.addLogger(StdOutSqlLogger)

        // ADD THIS - Create tables
        create (Cities)

        val stPeteId = Cities.insert {
            it[name] = "St. Petersburg"
        } get Cities.id

        println("Cities: ${Cities.selectAll()}")
    }
}

如果这可行,我敢打赌他们会接受 PR 作为他们的文件。

于 2018-01-20T17:45:31.080 回答