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