Grails select domain objects based on an enum valu

2019-02-27 19:10发布

问题:

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 all 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.

回答1:

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])


回答2:

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.



标签: grails gorm