首页 > 解决方案 > 使用数据库时无法执行事务块。科特林,暴露

问题描述

我正在尝试使用 Kotlin 的 Exposed 框架将事务发送到 Postgre 数据库(表存在),但是发生了一个错误,不允许我这样做。错误出现在行SchemaUtils.create(tableTest)

源代码:

import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction

fun main(args: Array<String>) {
    val db = Database.connect("jdbc:postgresql://localhost:5432/testBase", driver = "org.postgresql.Driver", user = "user", password = "123")
    println("Database name: ${db.name}")
    transaction {
        addLogger(StdOutSqlLogger)
        SchemaUtils.create(tableTest)
        println("People: ${tableTest.selectAll()}")
    }
}

object tableTest: Table() {
    val id = integer("id")
    val name = text("name")
    val surname = text("surname")
    val height = integer("height")
    val phone = text("phone")

    override val primaryKey = PrimaryKey(id)
}

错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at MainKt$main$1.invoke(main.kt:12)
    at MainKt$main$1.invoke(main.kt)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:170)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:211)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:219)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:210)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:148)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:219)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:120)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:118)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:117)
    at MainKt.main(main.kt:10)
Caused by: java.lang.IllegalStateException: javaClass.`package` must not be null
    at org.jetbrains.exposed.sql.Table.<init>(Table.kt:306)
    at org.jetbrains.exposed.sql.Table.<init>(Table.kt:303)
    at tableTest.<init>(main.kt:30)
    at tableTest.<clinit>(main.kt:30)
    ... 12 more

build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.4.0"
    application
}
group = "me.amd"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    jcenter()
}
dependencies {
    testImplementation(kotlin("test-junit"))
    implementation("org.jetbrains.exposed", "exposed-core", "0.26.2")
    implementation("org.jetbrains.exposed", "exposed-dao", "0.26.2")
    implementation("org.jetbrains.exposed", "exposed-jdbc", "0.26.2")
    implementation("org.postgresql:postgresql:42.2.16")
    implementation("org.slf4j", "slf4j-api", "1.7.25")
    implementation("org.slf4j", "slf4j-simple", "1.7.25")

    implementation("org.xerial:sqlite-jdbc:3.30.1")
}
tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "1.8"
}
application {
    mainClassName = "MainKt"
}

尝试这样做:

transaction {
    addLogger(StdOutSqlLogger)
    val schema = Schema("tableTest", authorization = "postgres", password = "123456")
    SchemaUtils.setSchema(schema)
    println("People: ${tableTest.selectAll()}")
}

但错误已移至该行println("People: ${tableTest.selectAll()}")

我试图向 SQLite 发送查询——一切都是一样的

如何修复此错误并仍然向数据库发送请求?我希望能得到你的帮助!

标签: databasekotlintransactionskotlin-exposed

解决方案


在您的导入语句上方添加一个包语句。此外,在类中添加您的主要方法。


推荐阅读