I've very much enjoyed being able to shorten my code by using reference
in my Table
objects as well as referencedOn
and referrersOn
in my Entitie
s. But as I've gotten further with my project, I've realized that I might have to undo all that work and recreate these processes manually in the event that the rows being referenced are deleted.
Is there any way of keeping these without risking IllegalStateException
s (like being able to provide a default foreign key you know exists), or will I have to give it all up for manual reference methods?
Here is a minimal example:
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
}
Once the list of time sheets tries to print a row where a Employee UID doesn't match any Employee rows, it'll throw:
Exception in thread "main" java.lang.IllegalStateException: Cannot find employeeTable WHERE id=some-invalid-id-string