Retrieving JPA Entity List and metadata

11,651

Solution 1

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.

Solution 2

Entities are independent of any persistence unit when they are defined. They are associated with a persistence unit in its definition, in persistence.xml. EntityManagerFactory that manages the entity managers for a given persistent unit doesn't seem to have any API to get the list of entities it manages. SessionFactory of Hibernate, counterpart of EntityManagerFactory, does have APIs to get hold of the class metadata (http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/SessionFactory.html).

Share:
11,651

Related videos on Youtube

Frank Orellana
Author by

Frank Orellana

Sr. Developer with 10+ years of experience developing mainly with Meta4 PeopleNet (ERP) and Oracle Tools (Java, Pl-Sql, Developer, etc), also proficient with typescript, vuejs among others

Updated on September 15, 2022

Comments

  • Frank Orellana
    Frank Orellana over 1 year

    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!

  • Frank Orellana
    Frank Orellana over 11 years
    I'm not sure of this, I found documentation saying that the metamodel only worked for jpql queries, do you have any example of this?
  • Frank Orellana
    Frank Orellana over 11 years
    I guess the SessionFactory.getAllCollectionMetadata would do it (I looked into it and it retrieves a map of AbstractEntityPersister objects, which seems to have all the info I need) I will still wait to see if there's any JPA direct method or an EclipseLink one before I mark this as the correct answer though... Thanks!
  • Chris
    Chris over 11 years
    Metamodel is independent of JPQL and really used for criteria queries, though it can be used to access some of the information such as the entity names and attributes- metamodel.getEntities() and then EntityType.getName() and EntityType.getDeclaredAttributes() for each entity. This would be JPA data - table/column info requires provider specific code. See wiki.eclipse.org/EclipseLink/FAQ/… for how this might be done in EclipseLink
  • Frank Orellana
    Frank Orellana over 11 years
    Thanks, in the end I managed to use this solution, but just one thing, everytime I try to use it from a webapplication it throws a securityException, maybe is spring that is intercepting the calls, any idea on how to achieve this??
  • sunny_dev
    sunny_dev over 9 years
    Can you please share your code so that I can also take help from it. My requirement is when a select query is executed, it should retrieve records along with column name.