如何使用 Xodus DNQ 检查组合的唯一性
How to check for combined uniqueness with Xodus DNQ
我有两个实体; municipality
和 city
。市政当局被假定具有唯一的名称,而城市被假定为在其自治市内是唯一的。
有没有一种方法可以为城市设置一个约束,使其名称和城市名称的组合必须是唯一的?
实体
class XdCity(entity: Entity) : XdEntity(entity) {
companion object : XdNaturalEntityType<XdCity>()
var name by xdRequiredStringProp()
var municipality: XdMunicipality by xdLink1(
XdMunicipality::cities,
onDelete = OnDeletePolicy.CLEAR,
onTargetDelete = OnDeletePolicy.CASCADE
)
}
class XdMunicipality(entity: Entity) : XdEntity(entity) {
companion object : XdNaturalEntityType<XdMunicipality>()
var name by xdRequiredStringProp(unique = true)
val cities by xdLink1_N(
XdCity::municipality,
onDelete = OnDeletePolicy.CASCADE,
onTargetDelete = OnDeletePolicy.CLEAR
)
}
测试用例
@Test
fun testAddSameCityName() {
Database.store.transactional {
val municipality = XdMunicipality.new("Mun 1")
val city = XdCity.new("City")
city.municipality = municipality
}
// Allow insertion of same city name in other municipality
Database.store.transactional {
val municipality = XdMunicipality.new("Mun 2")
val city = XdCity.new("City")
city.municipality = municipality
}
// Do not allow insertion of existing city name in municipality
assertFailsWith<ConstraintsValidationException> {
Database.store.transactional {
val municipality = XdMunicipality.find("Mun 1")
val city = XdCity.new("City")
city.municipality = municipality
}
}
}
建议的方法是在 XdCity 和 XdMunicipality 之间使用 xdParent
和 xdChildren
关系。
当数据库有历史时,改变关系可能不是那么容易。要解决市政范围内唯一名称的问题,您可以对实体类型使用复合索引,如下所示:
companion object : XdNaturalEntityType<XdCity>() {
override val compositeIndices = listOf(
listOf(XdCity:: municipality, ...may goes anything like XdCity::name)
)
}
我有两个实体; municipality
和 city
。市政当局被假定具有唯一的名称,而城市被假定为在其自治市内是唯一的。
有没有一种方法可以为城市设置一个约束,使其名称和城市名称的组合必须是唯一的?
实体
class XdCity(entity: Entity) : XdEntity(entity) {
companion object : XdNaturalEntityType<XdCity>()
var name by xdRequiredStringProp()
var municipality: XdMunicipality by xdLink1(
XdMunicipality::cities,
onDelete = OnDeletePolicy.CLEAR,
onTargetDelete = OnDeletePolicy.CASCADE
)
}
class XdMunicipality(entity: Entity) : XdEntity(entity) {
companion object : XdNaturalEntityType<XdMunicipality>()
var name by xdRequiredStringProp(unique = true)
val cities by xdLink1_N(
XdCity::municipality,
onDelete = OnDeletePolicy.CASCADE,
onTargetDelete = OnDeletePolicy.CLEAR
)
}
测试用例
@Test
fun testAddSameCityName() {
Database.store.transactional {
val municipality = XdMunicipality.new("Mun 1")
val city = XdCity.new("City")
city.municipality = municipality
}
// Allow insertion of same city name in other municipality
Database.store.transactional {
val municipality = XdMunicipality.new("Mun 2")
val city = XdCity.new("City")
city.municipality = municipality
}
// Do not allow insertion of existing city name in municipality
assertFailsWith<ConstraintsValidationException> {
Database.store.transactional {
val municipality = XdMunicipality.find("Mun 1")
val city = XdCity.new("City")
city.municipality = municipality
}
}
}
建议的方法是在 XdCity 和 XdMunicipality 之间使用 xdParent
和 xdChildren
关系。
当数据库有历史时,改变关系可能不是那么容易。要解决市政范围内唯一名称的问题,您可以对实体类型使用复合索引,如下所示:
companion object : XdNaturalEntityType<XdCity>() {
override val compositeIndices = listOf(
listOf(XdCity:: municipality, ...may goes anything like XdCity::name)
)
}