1

我有以下 DAO 类

package exposed.example

import org.jetbrains.exposed.dao.EntityID
import org.jetbrains.exposed.dao.UUIDEntity
import org.jetbrains.exposed.dao.UUIDEntityClass
import org.jetbrains.exposed.dao.UUIDTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
import java.util.*

object CustomerTable: UUIDTable(name = "customer") {
    val name = varchar(name = "name", length = 255).uniqueIndex()
}

class CustomerDAO(id: EntityID<UUID>): UUIDEntity(id) {
    companion object : UUIDEntityClass<CustomerDAO>(CustomerTable)

    var name by CustomerTable.name
}

object OrderTable: UUIDTable(name = "orders") {
    val customer = reference(name = "customer_id", foreign = CustomerTable)
    val product = varchar(name = "product", length = 255)
}

class OrderDAO(id: EntityID<UUID>): UUIDEntity(id) {
    companion object : UUIDEntityClass<OrderDAO>(OrderTable)

    var customer by OrderTable.customer
    var product by OrderTable.product
}

如果我手动创建事务,那么实体会保留在数据库中,请参见下面的代码

fun main(args: Array<String>) {
    Database.connect("jdbc:postgresql://localhost:5432/testdb", driver = "org.postgresql.Driver", user = "test", password = "testpassword")
    transaction {
        SchemaUtils.create(CustomerTable, OrderTable)
        val customer = CustomerDAO.new {
            name = "Alice_${System.currentTimeMillis()}"
        }

        OrderDAO.new {
            this.customer = customer.id
            product = "MegaProduct"
        }
    }
}

但是,如果我在 Spring Boot 应用程序的 @Transactional 方法中使用相同CustomerDAOOrderDAO方法,就会开始发生奇怪的事情。

package exposed.example

import org.jetbrains.exposed.spring.SpringTransactionManager
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
import org.springframework.transaction.annotation.EnableTransactionManagement
import org.springframework.transaction.annotation.Transactional
import javax.sql.DataSource


open class Service {

    @Transactional
    open fun createCustomer(name: String): CustomerDAO {
        return CustomerDAO.new {
            this.name = name
        }
    }

    @Transactional
    open fun createOrder(customer: CustomerDAO, product: String): OrderDAO {
        return OrderDAO.new {
            this.customer = customer.id
            this.product = product
        }
    }

    @Transactional
    open fun doBoth(name: String, product: String): OrderDAO {
        return createOrder(createCustomer(name), product)
    }

}

@SpringBootApplication
@EnableTransactionManagement
open class App {

    @Bean
    open fun transactionManager(dataSource: DataSource) = SpringTransactionManager(dataSource)

    @Bean // PersistenceExceptionTranslationPostProcessor with proxyTargetClass=false, see https://github.com/spring-projects/spring-boot/issues/1844
    open fun persistenceExceptionTranslationPostProcessor() = PersistenceExceptionTranslationPostProcessor()

    @Bean
    open fun service() = Service()

}

fun main(args: Array<String>) {

    val app = runApplication<App>(*args)
    val service = app.getBean(Service::class.java)

//    val customer = service.createCustomer("Alice1")
//    service.createOrder(customer, "SpringProduct")

    service.doBoth("Bob", "SpringProduct")

}

在这种情况下,仅customer创建但不创建。order如果我取消注释上面的两行,则不会创建客户,第二行将导致 NPE。

所以在@Transactional 方法中,实体只有在被另一个引用或被查询时才会被持久化。

无论如何,如何让它坚持下去?

提前致谢,

4

2 回答 2

0

到目前为止,我已经找到了这个解决方法:

@Transactional
    open fun createOrder(customer: CustomerDAO, product: String): OrderDAO {
        val id = OrderTable.insertAndGetId {
            it[this.customer] = customer.id
            it[this.product] = product
        }
        return OrderDAO.findById(id)!!
    }

但它是 DSL 和 DAO API 的肮脏组合,任何改进都值得赞赏。

于 2018-05-21T08:55:24.600 回答
0

问题在暴露的 0.10.2 中消失了

于 2018-06-21T08:18:49.890 回答