class VendorService : Controller() { private val registry = StandardServiceRegistryBuilder().configure().build() private val sessionFactory = MetadataSources(registry).buildMetadata().buildSessionFactory() init { insertTestData() } fun shutdown() { sessionFactory.close() } private fun insertTestData() { saveVendor(Vendor("Oracle")) saveVendor(Vendor("Microsoft")) } fun listVendors() = withTransaction { (createQuery("from Vendor").list() as List).observable() } fun saveVendor(vendor: Vendor) = withTransaction { save(vendor) } fun deleteVendor(vendor: Vendor) = withTransaction { delete(vendor) } private fun withTransaction(fn: Session.() -> T): T { val session = sessionFactory.openSession() session.beginTransaction() try { return fn(session) } finally { session.transaction.commit() session.close() } } } class MyApp: App(MainView::class) { val vendorService: VendorService by inject() override fun stop() { vendorService.shutdown() super.stop() } }