0

Let's say we have an "Organization" table. The "Organization" has a foreign key pointing to another "Organization" row within the same table as its parent. In Jetbrains Exposed, the one-to-one self-referencing relationship can be declared as:

object OrganizationTable : IntIdTable () {
    val name = varchar("name", 256)
    .
    .
    .
    val parent = reference("parent", OrganizationTable, ReferenceOption.RESTRICT).nullable()
}

How do I do the same to the counterpart DAO entity declaration? I got errors using the following declaration:

class OrganizationEntity(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<OrganizationEntity>(OrganizationTable)
    var name by OrganizationTable.name
    .
    .
    .
    var parent by  ? ? ? ? ? ?  // error using: by OrganizationEntity referencedOn OrganizationTable.parent
}

Thanks in advance!

  • Please note that reference("parent", ...) has to be declared nullable(). The compiler is fine if this reference isn't declared nullable. However, I expect the entity declaration to have "parent" as a nullable OrganizationEntity in that top level organizations are expected to have "null parent". – hytparadisee Jan 17 '22 at 08:04

1 Answers1

0

Okay okay I overlooked the documentation, there is the optionalReferencedOn() which is the nullable counterpart of referencedOn(). Cheers!