Grails select domain objects based on an enum value in an enum list property
I'm having trouble selecting items from a list of domain objects based on a value in an enum list.
My domain object looks like this:
class Truck {
static hasMany = [ makes: Make ]
}
where a Make looks like this:
enum Make {
KENWORTH, MACK, VOLVO
}
I'm not really sure how do something like Truck.findByMake(Make.MACK) to give me 开发者_如何学Pythonall of the Trucks that have this Make in their list of Makes. That call gives me this error:
No property found for name [make] for class [class Truck]
Any ideas? Grails 1.2.2.
This one's tricky and not supported by the dynamic finders. I also don't know how to do this with Criteria queries, but the HQL would be
def mackTrucks = Truck.executeQuery(
'select t from Truck t left join t.makes make where make=:make',
[make: Make.MACK])
You can make ist with criteria query the answer is her in the forum but you have to customize it. Maybe like this:
Truck.createCriteria.list ={makes{eq('name', Make.MACK)}
}
I think each Enum has the attribute name.
精彩评论