Grails select domain objects based on an enum value in an enum list property

落爺英雄遲暮 提交于 2019-12-02 13:40:02

问题


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.



来源:https://stackoverflow.com/questions/4829823/grails-select-domain-objects-based-on-an-enum-value-in-an-enum-list-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!