user800014
user800014

Reputation:

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.

Upvotes: 0

Views: 1725

Answers (1)

Pavel Vlasov
Pavel Vlasov

Reputation: 4331

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.

Upvotes: 1

Related Questions