kotlin exposed failed(插入成功,但select失败)

kotlin exposed failed (insert is successful, but select is failure)

感谢阅读这个问题。

我创建了简单的 kotlin 项目,我想学习 kotlin exposed

我用的是H2数据库

我写了如下代码。

package learn.exposed.tables

import org.jetbrains.exposed.sql.Table

object AuthorTable : Table("author") {

    val name = varchar("name", 30)
}
fun main() {
    // this url based on http://www.h2database.com/html/features.html#execute_sql_on_connection
    val url = "jdbc:h2:mem:test;INIT=runscript from 'classpath:/create.sql'\;runscript from 'classpath:/init.sql'"
    Database.connect(url, driver = "org.h2.Driver", user = "root", password = "")

    transaction {
        AuthorTable.insert {
            it[name] = "hoge"
        }
        println("insert done.") // this message can show on console. I think Insert is successfull.
    }

    transaction {
        AuthorTable.selectAll().firstOrNull()
    }
}

和 sql 个文件。

create table author (name varchar(30));

insert into author values ('author1');

执行main()时,控制台显示insert done.。简而言之,我认为 insert 做得很好,但是当执行 AuthorTable.selectAll().firstOrNull() 时,会发生如下所示的异常,

Exception in thread "main" org.jetbrains.exposed.exceptions.ExposedSQLException: org.h2.jdbc.JdbcSQLNonTransientException: 一般エラー: "java.lang.NullPointerException"
General error: "java.lang.NullPointerException" [50000-200]
SQL: [Failed on expanding args for SELECT: org.jetbrains.exposed.sql.Query@27406a17]
    at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:62)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:135)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:121)
    at org.jetbrains.exposed.sql.AbstractQuery.iterator(AbstractQuery.kt:65)
    at kotlin.collections.CollectionsKt___CollectionsKt.firstOrNull(_Collections.kt:267)
    at learn.exposed.MainKt$main.invoke(Main.kt:22)
    at learn.exposed.MainKt$main.invoke(Main.kt)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction$run(ThreadLocalTransactionManager.kt:179)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.access$inTopLevelTransaction$run(ThreadLocalTransactionManager.kt:1)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction.invoke(ThreadLocalTransactionManager.kt:205)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:213)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:204)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction.invoke(ThreadLocalTransactionManager.kt:156)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:213)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:126)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:123)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:122)
    at learn.exposed.MainKt.main(Main.kt:21)
    at learn.exposed.MainKt.main(Main.kt)

我能解决这个问题吗?你知道如何解决这个问题吗?

谢谢。

由于 H2 错误,您似乎至少需要一个主键 (PK) 或约束。

https://github.com/h2database/h2database/issues/2191

https://github.com/JetBrains/Exposed/issues/801