Grails - Need to restrict fetched rows based on condition on join table

Posted by sector7 on Stack Overflow See other posts from Stack Overflow or by sector7
Published on 2010-04-20T15:42:23Z Indexed on 2010/04/20 15:43 UTC
Read the original article Hit count: 218

Filed under:
|
|
|
|

Hi guys,

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 a new 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 drivers 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!

© Stack Overflow or respective owner

Related posts about grails

Related posts about groovy