문제

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.
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top