Grails - 需要根据连接表的条件限制获取的行
我有两个域 Car 和 Driver,它们具有多对多关系。这种关联是在表tblCarsDrivers中定义的,毫不奇怪,它具有两个表的主键,但另外还有另一个布尔字段deleted。问题就在这里。当我在域 Car 上查找/获取查询时,我会获取所有相关驱动程序,无论它们在 tblCarsDrivers 中的删除状态如何,这是预期的。
我需要添加一个子句/约束 从删除的关联中排除 获取的记录列表。
PS:我尝试在 joinTable 名称中使用关联域 CarDriver,但这似乎不起作用。显然它只需要表名,而不是地图。
PPS:我知道除了映射表中的映射键之外还有任何其他字段是不自然的,但这就是我获取它的方式,并且无法更改。
汽车域是这样定义的 -
class Car {
Integer id
String name
static hasMany = [drivers:Driver]
static mapping = {
table 'tblCars'
version false
drivers joinTable:[name: 'tblCarsDrivers',column:'driverid',key:'carid']
}
}
谢谢!
I have these two domains Car and Driver which have many-to-many relationship. This association is defined in table tblCarsDrivers which has, not surprisingly, primary keys of both the tables BUT additionally also has another boolean field deleted. Herein lies the problem. When I find/get query on domain Car, I am fetched all related drivers irrespective of their deleted status in tblCarsDrivers, which is expected.
I need to put a clause/constraint to
exclude the deleted associations from the
list of fetched records.
PS: I tried using an association domain CarDriver in joinTable name but that seems not to work. Apparently it expects only table names, not maps.
PPS: I know its unnatural to have any other fields besides the mapping keys in mapping table but this is how I got it and it cant be changed.
Car domain is defined as such -
class Car {
Integer id
String name
static hasMany = [drivers:Driver]
static mapping = {
table 'tblCars'
version false
drivers joinTable:[name: 'tblCarsDrivers',column:'driverid',key:'carid']
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一点也不罕见。如果您想存储有关关系的属性,这是显而易见的解决方案。您应该恢复关联域 CarDriver,除了与 Car 和 Driver 的关系之外,它还具有已删除的属性,然后您应该能够编写排除已删除驱动程序的查询。
此处提供了如何定义此类映射的综合示例< /a>.
This is not at all unusual. If you want to store properties about the relationship, this is the obvious solution. You should reinstate your association domain CarDriver which has a deleted property in addition to a relationship to Car and Driver, and you should then be able to write a query which excludes the deleted drivers.
A comprehensive example of how to define such a mapping is provided here.