按照Exposed上提供的示例,我无法在创建它的事务之外读取创建的表/数据。我正在使用 h2-in-memory 数据库。
例外是:
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Table "CITIES" not found; SQL statement:
我添加了一个提交的调用,但这没有帮助。如果我在创建数据的事务中读取数据,如 github 链接上的示例所示,它可以正常工作。这是它的简化版本:
fun main(args: Array<String>) {
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
transaction {
create(Cities)
City.new {
name = "St. Petersburg"
}
println("Cities: ${City.all().joinToString { it.name }}")
//I have added this commit here
commit()
}
//I want to read the data outside the transaction, but it does not work
transaction {
println("Cities: ${City.all().joinToString { it.name }}")
}
}
我怎样才能持久化数据?
添加logger.addLogger(StdOutSqlLogger)
给出以下输出:
SQL: CREATE TABLE IF NOT EXISTS CITIES (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(50) NOT NULL)
SQL: INSERT INTO CITIES (NAME) VALUES ('St. Petersburg')
SQL: SELECT CITIES.ID, CITIES.NAME FROM CITIES