Hi jaejong
Unfortunately, hibernate can't reach your goal.
(1) Reason:The reason is Java Reflection API iterators the members of class with unpredictable order
Code:
// Iterates members with unpredictable order.
for (Field field : YourType.class.getDeclaredFields()) {
System.out.println("Found field: " + field.getName());
}
Hibernate uses Java Reflection to create the metadata of Entity class, so that the order of fields in metadata and the order of SQL columns are unpredictable.
(2) Is it possible to resolve this problem?Yes, it can be resolved, don't use the Java Reflection API, read the bytecode(class file) directly. The order of members in bytecode is same with the declaration order in the source code. like this
First: add maven dependency to add ASM(an open source library to read/write bytecode), here we use ASM5, not ASM4
Code:
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-all<artifactId>
<version>5.0.4</version>
</dependency>
Then: iterators all the fields:
Code:
// Iterates members with concurrent order, by reading bytecode file directly.
ClassReader reader = ClassReader(YourType.class.getName(), ClassReader.SKIP_CODE);
reader.accpet(new ClassVisitor(Opcodes.ASM_5, null) {
@Override
public void visitField(int acc, String name, String desc, String signature) {
System.out.println("Found field: " + name);
}
});
your problem can be resolved if Hibernate uses this way to build metadata for Entity class in the future.