Could you please tell me how to do a projection on a Grails domain class?! In my case, I want to get a list of (for example) User's name by their Ids. It means in my method, I pass a list of userId and get a list of user's names. Does dynamic methods of Groovy domain support this feature? At present, I'm using my below function:
public String getUserNamesByIds(String[] ids) {
StringBuffer names = User.get(Integer.parseInt(ids[0]).getName())
if(ids.length > 1) {
(1..ids.length - 1).each{
names.append(", " + User.get(Integer.parseInt(ids[it])).getName())
}
}
return names.toString()
}
As you see, I just want to get the name (and build the total string). I think it's not good because I have to do many small steps, and perform many queries to database to get the User object. Is there any better method to do this? Thank you so much!