Search code examples
javahibernatejpaeclipselink

Retrieving JPA Entity List and metadata


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!


Solution

  • 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.