Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all mapped Entities from EntityManager

I have a piece of maintenance code that should grant select privileges to a certain user at certain points of time:

grant select on A_DB.A_TABLE to READ_ONLY_USER;

I want to do this for all tables. I could use select * from tab in Oracle or show tables in MySQL to get the complete list and then move on like that.

But since I already have the javax.persistence.EntityManager Object at hand, I wondered if there is another way to get at all the mapped Entities, the Manager knows about (I am using Hibernate under the hood).

like image 323
raoulsson Avatar asked Jan 05 '10 15:01

raoulsson


People also ask

How do I get EntityManager from EntityManagerFactory?

Step 2: Obtaining an Entity Manager From a Factory EntityManager entityManager = entityManagerFactory. createEntityManager(); EntityManager — An EntityManager is an interface. createEntityManager() method — It creates a new application-managed EntityManager.

What is difference between EntityManagerFactory and EntityManager?

EntityManagerFactory vs EntityManagerWhile EntityManagerFactory instances are thread-safe, EntityManager instances are not. The injected JPA EntityManager behave just like an EntityManager fetched from an application server's JNDI environment, as defined by the JPA specification.

What is @PersistenceContext annotation used for?

You can use the @PersistenceContext annotation to inject an EntityManager in an EJB 3.0 client (such as a stateful or stateless session bean, message-driven bean, or servlet). You can use @PersistenceContext attribute unitName to specify a persistence unit by name, as Example 29-13 shows.


3 Answers

As of 2016 (Hibernate 5.2), both getAllClassMetadata and Configuration are deprecated.

I guess this could be used instead:

Set<EntityType<?>> entities = sessionFactory.getMetamodel().getEntities();

In special, to get the classes:

List<?> classes = entities.stream()
                          .map(EntityType::getJavaType)
                          .filter(Objects::nonNull)
                          .collect(Collectors.toList());
like image 56
MarcG Avatar answered Oct 19 '22 22:10

MarcG


There are two ways that I can see getting all of the mapped entities and their corresponding SQL tables (there may be others).

The most straightfoward is if you can use your Hibernate Configuration object:

    for(Iterator it = config.getClassMappings(); it.hasNext();){
        PersistentClass pc = (PersistentClass) it.next();
        System.out.println(pc.getEntityName() + "\t" + pc.getTable().getName());
    }

Alternatively, you can do a little more casting and get this same information out of the SessionFactory too:

    Map<String, ClassMetadata>  map = (Map<String, ClassMetadata>) sessionFactory.getAllClassMetadata();
    for(String entityName : map.keySet()){
        SessionFactoryImpl sfImpl = (SessionFactoryImpl) sessionFactory;
        String tableName = ((AbstractEntityPersister)sfImpl.getEntityPersister(entityName)).getTableName();
        System.out.println(entityName + "\t" + tableName);
    }
like image 24
BryanD Avatar answered Oct 19 '22 23:10

BryanD


as MarcG answer states the getAllClassMetadata() is deprecated since few years ago

As of Hibernate - 5.4.30.Final - released March 19th, 2021 The code below works and is not deprecated :

MetamodelImplementor metaModelImpl = (MetamodelImplementor)session.getMetamodel();
Map<String, EntityPersister> entityPersisters = metaModelImpl.entityPersisters();
Collection<EntityPersister> val = entityPersisters.values();               

for (EntityPersister ep : val) {
        AbstractEntityPersister aep = (AbstractEntityPersister)ep;

        System.out.println(aep.getTableName());
        System.out.println(Arrays.toString(aep.getIdentifierColumnNames()));
        for (String propName : aep.getPropertyNames()) {
               System.out.println(propName);
               System.out.println(Arrays.toString(aep.getPropertyColumnNames(propName)));
        }
 }
like image 40
M. Amer Avatar answered Oct 19 '22 23:10

M. Amer