0

我非常喜欢能够通过reference在我的Table对象以及我referencedOn的s 中使用来缩短我的代码。但是随着项目的深入,我意识到如果删除了引用的行,我可能必须撤消所有工作并手动重新创建这些流程。referrersOnEntitie

有什么方法可以保留这些而不冒风险IllegalStateException(例如能够提供您知道存在的默认外键),还是我必须放弃所有手动参考方法?

这是一个最小的例子:

fun main(args: Array<String>) {
    /* Connecting to DB here */
    transaction {
        val timesheets = TimeSheet.all()
        println("Printing time sheet list:")
        timesheets.forEach { sheet ->
            println("Time Sheet Object for employee ${sheet.employee}\n$sheet")
        }
    }
    /* Disconnecting to DB */
}

object EmployeeTable : IdTable<UUID>("employeeTable") {
    override val id = uuid("EmployeeUID").entityId().primaryKey()
}

object TimeSheetTable : IdTable<Int>("timeSheetTable") {
    override val id = integer("JobUID").primaryKey().autoIncrement().entityId()

    val employee = reference("EmployeeUID", EmployeeTable)
}

class Employee(id: EntityID<UUID>) : UUIDEntity(id) {
    companion object : UUIDEntityClass<Employee>(EmployeeTable)
}

class TimeSheet(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<TimeSheet>(TimeSheetTable)

    var employee by Employee referencedOn TimeSheetTable.employee
}

一旦时间表列表尝试打印员工 UID 与任何员工行都不匹配的行,它将抛出:

Exception in thread "main" java.lang.IllegalStateException: Cannot find employeeTable WHERE id=some-invalid-id-string
4

0 回答 0