Grails:在hasmany-relation中查找

Grails: find in hasmany-relation

我在 Grails 中有两个 类:

class Person {
    static hasMany = [tickets: Ticket]
}

class Ticket {
    String ticketStatus
}

ticketStatus 可以是描述票证的任何字符串。我如何才能找到拥有一张或多张特定状态票证的所有 Person 实例?

How can I find all instances of Person that have one ore more tickets with specific status?

一种常见的方法是使用 DetachedCriteria:

def results = Person.findAll {
    tickets {
        ticketStatus == 'some status you would like to search for'
    }
}

根据您使用的数据库和驱动程序,您可能会有所不同 SQL 但对于 H2,这是该查询将生成的内容:

select this_.id as id1_0_1_, this_.version as version2_0_1_, tickets3_.person_tickets_id as person_t1_1_, tickets_al1_.id as ticket_i2_1_, tickets_al1_.id as id1_2_0_, tickets_al1_.version as version2_2_0_, tickets_al1_.ticket_status as ticket_s3_2_0_ from person this_ inner join person_ticket tickets3_ on this_.id=tickets3_.person_tickets_id inner join ticket tickets_al1_ on tickets3_.ticket_id=tickets_al1_.id where tickets_al1_.ticket_status=?