Get all table names set up in SessionFactory

Here is howto getting one tableName with getClassMetadata

ClassMetadata cm = sessionFactory.GetClassMetadata(className);

AbstractEntityPersister aep = (AbstractEntityPersister) cm;
String tableName = aep.getTableName();

[EDIT] : you can find all by calling getAllClassMetadata() and find all table names like that

Map m = sessionFactory.GetAllClassMetadata();
/* iterate map*/
AbstractEntityPersister aep = m.get(/*key (className)*/) 
String tableName = aep.getTableName(); 

If you are using JPA instead of direct dependency on hibernate., following code should help in getting 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;
}

Tags:

Java

Hibernate