Daniel Pitts
10/13/2024, 4:55 PMmany
side doesn't have a unique identifier, only a reference to the one
side?Oleg Babichev
10/14/2024, 12:38 PMChantal Loncle
10/16/2024, 1:34 AMEntity
must have a unique identifier, for the reasons mentioned above. You can set up the tables in the database using DSL any way you want, but you wouldn't be able to take advantage of DAO entity references if the many
side doesn't have an entity:
object Provinces : IntIdTable("provinces") { // one
val name = varchar("name", 32)
}
object Cities : Table("cities") { // many
val name = varchar("name", 32)
val province = reference("province_id", Provinces.id)
}
class Province(id: EntityID<Int>) : IntEntity(id) {
var name by Provinces.name
val cities by ??? referrersOn Cities.province
companion object : IntEntityClass<Province>(Provinces)
}
There's nothing technically stopping the reference itself from being used as the unique identifier, if that is what you need:
object Provinces : IntIdTable("provinces") { // one
val name = varchar("name", 32)
}
object Cities : IdTable<Int>("cities") { // many
override val id = reference("province_id", Provinces)
val name = varchar("name", 32)
init {
// need to explicitly set id here
// since reference is being used instead of unique constraint
addIdColumn(id)
}
}
class Province(id: EntityID<Int>) : IntEntity(id) {
var name by Provinces.name
val cities by City referrersOn Cities.id
companion object : IntEntityClass<Province>(Provinces)
}
class City(id: EntityID<Int>) : IntEntity(id) {
var name by Cities.name
val province by Province referencedOn Cities.id
companion object : IntEntityClass<City>(Cities)
}
This may cause problems when caching City
entities, however, since caching relies on each entity instance having a unique id.
As requested above, any details about how both tables in the relation, and the foreign key, are defined in the database could give a better idea about how the entity(ies) could be mapped.Daniel Pitts
10/16/2024, 1:46 AMChantal Loncle
10/16/2024, 11:52 PMCompositeIdTable
with the same example above:
object Provinces : IntIdTable("provinces") { // one
val name = varchar("name", 32)
}
object Cities : CompositeIdTable("cities") { // many
val cityId = integer("city_id").entityId()
val provinceId = reference("province_id", Provinces)
val name = varchar("name", 32)
override val primaryKey = PrimaryKey(cityId, provinceId)
init {
// no need to add cityId here, as .entityId() does this automatically
addIdColumn(provinceId)
}
}
class Province(id: EntityID<Int>) : IntEntity(id) {
var name by Provinces.name
val cities by City referrersOn Cities // composite key referrersOn
companion object : IntEntityClass<Province>(Provinces)
}
class City(id: EntityID<CompositeID>) : CompositeEntity(id) {
var name by Cities.name
companion object : CompositeEntityClass<City>(Cities)
}
More details on configuring and using composite key tables can be found in DAO docs.maxmello
01/09/2025, 10:52 AMentityId()
on reference
column definitions, instead using addIdColumn
in the constructor? So if I have a composite table with 3 foreign key references that combined are the primary key, I would just use addIdColumn
three times, not using entityId()
at all?
And it is correct to explicitly not include composite key columns in the Entity, even for references, instead accessing them via the id
property?