Created
June 11, 2018 16:16
-
-
Save Tapac/8739f7918a347175296d9be25ac1159c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jetbrains.exposed.spring | |
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.SchemaUtils | |
import org.jetbrains.exposed.sql.transactions.transaction | |
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.jdbc.datasource.embedded.EmbeddedDatabase | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType | |
import org.springframework.transaction.annotation.EnableTransactionManagement | |
import org.springframework.transaction.annotation.Transactional | |
import java.util.* | |
import javax.sql.DataSource | |
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 | |
} | |
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) | |
} | |
@Transactional | |
open fun findOrderByProduct(product: String) : OrderDAO? { | |
return OrderDAO.find { OrderTable.product eq product }.singleOrNull()?.apply { | |
this.customer // load to cache | |
} | |
} | |
} | |
@SpringBootApplication | |
@EnableTransactionManagement | |
open class App { | |
@Bean | |
open fun ds(): EmbeddedDatabase = EmbeddedDatabaseBuilder().setName("embeddedTest").setType(EmbeddedDatabaseType.H2).build() | |
@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) | |
transaction { | |
SchemaUtils.create(CustomerTable, OrderTable) | |
} | |
val customer = service.createCustomer("Alice1") | |
service.createOrder(customer, "SpringProduct") | |
println(service.findOrderByProduct("SpringProduct")?.product) | |
// service.doBoth("Bob", "SpringProduct") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment