jrey
jrey

Reputation: 2183

JPA - Retrieve all Entity Classes

I need to have a List of clasess that are Persistence Entities, I need have Entity Information, using Reflection API of JPA

I have the EntityManager, But I do not know if that is the way.

I want to do a generic logging for my Entities using a EntityListener. That works well, but I do not have the way to register the listener to all my entities.

Upvotes: 2

Views: 1648

Answers (2)

Neil Stockton
Neil Stockton

Reputation: 11531

Use the JPA2 MetaModel? It has assorted methods to see the entities (or managed types).

 Set<javax.persistence.metamodel.EntityType<?>> entityTypes = entityManagerFactory.getMetamodel().getEntities();
    for (javax.persistence.metamodel.EntityType entityType : entityTypes){
        logger.info(entityType.getName());
        logger.info(entityType.getJavaType().getCanonicalName());
        logger.info("******************************");
    }

Upvotes: 4

Predrag Maric
Predrag Maric

Reputation: 24423

Take a look at Configuration#getClassMappings()

Returns: Iterator of the entity mappings currently contained in the configuration.

Upvotes: 1

Related Questions