开发者

Getting hibernate information about entites at runtime

开发者 https://www.devze.com 2023-02-22 16:14 出处:网络
I am seeking a way to get information about my hibernate entity classes at runtime. In detail I would need information like

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号