Search code examples
grailsgrails-orm

How can I access a domain property using the property name?


I have a domain object with a property called date:

class Item implements Comparable{
  Date date
}

How can I access that date doing something like:

Item.list().each{
  Date d = it.get("date")
}

I know that I could do Date d = it.date but I want to be able to generically pick a property from my domain object and access it without using .property.


Solution

  • Try this..,.

    Item.list().each {
        Date d = it.properties.get("date")
    }
    

    or

    Item.list().each {
        Date d = it.getProperty("date")
    }