Search code examples
grailsgrails-ormgrails-2.0

Get Grails domain persistent properties in order


Is there some way to retrieve the persistent properties of a Domain Class in the same order I declared them in the class?

class MyDomainClass {
  String prop1
  String prop2
  String prop3
}


def domainClass = grailsApplication.getDomainClass(MyDomainClass)
def props = domainClass.persistentProperties //this not retrieve them in order.

Solution

  • Class::getDeclaredFields does the job. Once it is compiled by the oracle's javac 7 and run by oracle's jvm 7 you will get the result, but I don't think you are guaranteed to get it tomorrow:

    "The elements in the array returned are not sorted and are not in any particular order".

    class MyDomainClass {
      String name
      String hobby
      int age
    }
    MyDomainClass.class.getDeclaredFields().each { println it.name }
    

    Output:

    name
    hobby
    age
    $staticClassInfo
    __$stMC
    

    Anyway that's all not too good. One could use annotations supplying the ordering information.