Hakon Grotte
12/07/2022, 1:02 PMoptionalBackReferencedOn
reference in the following example?
Definitions (I have modified the actual object names and definitions):
// tables (DSL)
object PersonTable : IdTable<UUID>("person") {
override val id: Column<EntityId<UUID>> = uuid("person_id").entityId()
val name = varchar("name", 255)
}
object AddressInformation : IdTable<UUID>("address") {
override val id: Column<EntityId<UUID>> = reference("address_id", PersonTable.id) // from this table's perspective the reference is not optional
val address = varchar("address", 255)
}
// entities (DAO)
class PersonEntity(id: EntityID<UUID>) : Entity<UUID>(id) {
companion object : EntityClass<UUID, PersonEntity>(PersonTable)
var name by PersonTable.name
val addressInformation by AddressEntity optionalBackReferencedOn AddressInformation.id
}
class AddressEntity(id: EntityID<UUID>) : Entity<UUID>(id) {
companion object : EntityClass<UUID, AddressEntity>(AddressInformation)
var address by AddressInformation.address
}
I want to select all people matching the same name
and include the optional reference addressInformation. Moreover, I want to use a single query with join for this. I read online that wrapRows
can be used for this:
Transaction:
PersonTable.leftJoin(AddressInformation)
.select { PersonTable.name eq name }
.map { PersonTable.wrapRow(it).toDTO() }
Inside my toDTO()
I do select the PersonEntity.addressInformation
. This makes the DAO api run an additional select query to fetch this field. How can I avoid this additional query from being run?Hakon Grotte
12/07/2022, 1:24 PMoptionalBackReferencedOn
and is quite ugly:
Inside final map:
.map {
val addressEntity = try {
AddressEntity.wrapRow(it)
} catch (e: NullPointerException) { null }
val personEntity = PersonEntity.wrapRow(it)
personEntity.toDto(addressEntity)
}
, but only uses 1 SQL select queryGustav Elmgren
12/08/2022, 4:53 PMwrapRow
(i think it exists one on the DTO class)?Gustav Elmgren
12/08/2022, 4:55 PMHakon Grotte
12/09/2022, 8:59 AMResultRow
to the DTO
, is that correct? I do, however, wonder if it is possible to avoid dealing with the ResultRow explicitly?