检索JPA实体列表和元数据 [英] Retrieving JPA Entity List and metadata

查看:658
本文介绍了检索JPA实体列表和元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有方法可以获得JPA中特定持久单元的所有实体类及其元数据。

通过元数据,我的意思不仅是字段,还包括它们的列名,长度,精度,数据类型,以及表名和我能得到的任何东西。我尝试了元模型,但我认为这仅仅是JPQL查询。



我需要能够向用户显示某些PU的所有活动实体,而我不想在某些数组或数据库中对它们进行硬编码,我希望API告诉我它具有哪些实体。另外,如果可能的话,为每个实体获取托管实例。

我想我可以尝试使用反射来获取具有@Entity注释的所有类,但是它不会很漂亮,并且知道哪些属于特定的PU会更困难,所以如果api已经公开了这个信息,它将会很棒。



我会更喜欢符合JPA的解决方案,但如果不可行,Hibernate或EclipseLink特定的答案就可以做到。

你可以从 EntityManager

$ b获取所有实体和相应的元数据信息。
$ b

EntityManager.getMetamodel()将使您可以访问 Metamodel 您可以访问 EntityType ManagedType 以获取实体的属性。


I wanted to know if there is a way to get all the Entities classes and their metadata for a specific persistent unit in JPA.

By metadata I mean not only the fields, but also their column name, lenght, precision, datatype, and also the table name and anything that I can get. I tried with the metamodel but I think that's more for the JPQL queries only.

I need to be able to show the user all the active Entities for some PU, and I don't want to hardcode them in some array or in a database, I want the API to tell me what Entities does it have. And also, if it is possible, get the managed instances for every entity.

I guess I could try using reflection to get all the classes with the @Entity annotation, but it would not be pretty, and it would be harder to know which belongs to a specific PU, so if the api already exposes this info it would be great.

I would prefer a JPA compliant solution but if not possible, an Hibernate or EclipseLink specific answer would do it.

Thanks!

解决方案

Yes you can get all the entities and the corresponding meta data information about the enitities from EntityManager.

EntityManager.getMetamodel() will give you access to the Metamodel interface from where you can access EntityType and ManagedType to get the attributes of the entity.

这篇关于检索JPA实体列表和元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆