views:

44

answers:

2

I wonder if there is a way to retreive domain instances as a Map where the key is the id of the domain object.

more specific i would like to do myDomainObject.list() to return a Map instead of a List.

+2  A: 

You can create this method easily in your domain class, for example:

class Person {

   String name

   static Map<Long, Person> mapAll() {
      def map = [:]
      list().each { map[it.id] = it }
      map      
   }
}
Burt Beckwith
Thanks. I thought maybe a built in function existed. but this is a great solution.
Iman
A: 

If this isn't the only place you need it, you could also use Burts code to extend eiter domain classes or the map itself via the ExpandoMetaClass.

This would centralize the functionality and keep your domain classes clean.

air_blob