Getting hibernate information about entites at runtime
I am seeking a way to get information about my hibernate entity classes at runtime. In detail I would need information like
Class ABC has the properties a, b and c with c being a list and association to class XYZ
for the class
开发者_开发问答public class ABC {
private int a;
private String b;
@OneToMany
Private List<XYZ> c;
}
All that is needed at runtime. I am sure Hibernate knows all that at runtime, but I am not aware how to get these information.
If you are using Hibernate as a JPA 2 provider, then you can use the JPA 2 metamodel(javax.persistence.metamodel
). This will give you information about your JPA entities from the JPA metadata:
entityManager.getMetamodel().entity(entityClass). getAttributes();
PersistentClass userMapping = cfg.getClassMapping(ABC.class);
cfg is an instance of Hibernate Configuration.
PersistentClass has all the info you need.
Looks like reflection is what you are after, its not hibernate specific information you seem to be after. You just want information about what fields the class has and what types they are...
You can get this information using reflection.
Class abcClass= ABC.class;
Field[] fields = abcClass.getDeclaredFields();
精彩评论