6

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

Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
raoulsson
  • 14,978
  • 11
  • 44
  • 68
  • You can check this:: http://stackoverflow.com/questions/43604928/hibernate-upgrade-to-5-2-session-factory-creation-and-replacing-persistentclas/43718626 – Hasan K May 06 '17 at 20:09

5 Answers5

15

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());
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
  • Migreting from HIbernate 3 to 5.2.10. I need to get from "entities" as you defined, to PersistentClass. Is that possible? – Vering Apr 26 '17 at 10:13
  • See my answer here: http://stackoverflow.com/questions/32780664/hibernate-migration-from-4-3-x-to-5-x-for-method-org-hibernate-cfg-configuration/43653061#43653061 – Vering Apr 27 '17 at 09:48
  • what's with the null check, when do we ever get javatypes that are null from the method getJavaType? – girgen Oct 05 '18 at 07:09
10

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);
    }
BryanD
  • 1,897
  • 12
  • 13
6

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)));
        }
 }
M. Amer
  • 916
  • 10
  • 12
1

If you have javax.persistence.EntityManager in hand., following method can help you get list all table names:

private List<String> getAllTables() {
    List<String> tableNames = new ArrayList<>();
    Session session = entityManager.unwrap(Session.class);
    SessionFactory sessionFactory = session.getSessionFactory();
    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();
        tableNames.add(tableName);
    }
    return tableNames;
}
Sairam Krish
  • 10,158
  • 3
  • 55
  • 67
0

Check what's available in this Map:

((Session) entityManager.getDelegate()).getSessionFactory().getAllClassMetadata() ;
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • The class metadata is really limited to the properties of the entity itself and doesn't contain any mapping-related data. This would be fine if the DB tables are named the same as the Entities. – BryanD Jan 05 '10 at 16:00
  • well, it looks like getAllClassMetadata was the solution sought. Your answer was more detailed, of course.. I should perhaps not give hints, but complete solutions. Still, I feel there's something wrong with that :) – Bozho Jan 05 '10 at 21:14