Grails - 需要根据连接表的条件限制获取的行

发布于 2024-08-29 12:53:53 字数 755 浏览 2 评论 0原文

我有两个域 CarDriver,它们具有多对多关系。这种关联是在表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2024-09-05 12:53:53

我知道拥有其他的东西是不自然的
除了映射键之外的字段
映射表,但这就是我得到它的方式
并且无法更改。

这一点也不罕见。如果您想存储有关关系的属性,这是显而易见的解决方案。您应该恢复关联域 CarDriver,除了与 Car 和 Driver 的关系之外,它还具有已删除的属性,然后您应该能够编写排除已删除驱动程序的查询。

此处提供了如何定义此类映射的综合示例< /a>.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文