我有以下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 app中的@Transactional方法中使用相同的CustomerDAO
和OrderDAO
,那么奇怪的事情就会开始发生。
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方法中,只有在被其他人引用或查询时,实体才会被持久化。
如何让它坚持下去?
提前致谢,
答案 0 :(得分: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的混合物,任何改进都会受到赞赏。
答案 1 :(得分:0)
问题在暴露的0.10.2中消失了