在一个事务中创建实体的两个实例失败

时间:2019-01-26 07:41:46

标签: kotlin-exposed

我是Kotlin的新手。我只有一个DAO(实体):User

class User(id: EntityID<Int>) : IntEntity(id) {
     companion object : IntEntityClass<User>(Users)
}

object Users : IntIdTable() {
}

现在考虑这段简单的代码:

transaction {
    val u1 = User.new(123) {}
    val u2 = User.new(1234) {}
}

它似乎在刷新时抛出以下异常:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at org.jetbrains.exposed.sql.statements.InsertStatement.processResults(InsertStatement.kt:38)
    at org.jetbrains.exposed.sql.statements.InsertStatement.executeInternal(InsertStatement.kt:103)
    at org.jetbrains.exposed.sql.statements.InsertStatement.executeInternal(InsertStatement.kt:13)
    at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:59)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:128)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:122)
    at org.jetbrains.exposed.sql.statements.Statement.execute(Statement.kt:29)
    at org.jetbrains.exposed.sql.QueriesKt.batchInsert(Queries.kt:90)
    at org.jetbrains.exposed.sql.QueriesKt.batchInsert$default(Queries.kt:60)
    at org.jetbrains.exposed.dao.EntityCache.flushInserts$exposed(Entity.kt:432)
    at org.jetbrains.exposed.dao.EntityCache.flush(Entity.kt:386)
    at org.jetbrains.exposed.dao.EntityCache.flush(Entity.kt:378)
    at org.jetbrains.exposed.sql.Transaction.flushCache(Transaction.kt:85)
    at org.jetbrains.exposed.sql.Transaction.commit(Transaction.kt:62)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:105)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:75)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:58)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:58)
    at ir.sls.mono.channel.table.TablesKt.main(Tables.kt:23)

为什么?问题与自动增量主键有关吗?

更新:

我的数据源定义是这样的:

hikari = HikariDataSource()
hikari.jdbcUrl = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"
hikari.maximumPoolSize = 2

我这样将数据源更改为MySql:

hikari.jdbcUrl = "jdbc:mysql://localhost:3306/mono?user=root&password=root"

代码成功了!!!!!

0 个答案:

没有答案