Get all table names set up in SessionFactory

前端 未结 4 1281
你的背包
你的背包 2020-12-16 05:52

Is there a way to retrieve the name of all tables that are managed by the SessionFactory? For instance, all the tables that were added via AnnotationConfi

4条回答
  •  星月不相逢
    2020-12-16 06:25

    If you are using JPA instead of direct dependency on hibernate., following code should help in getting all table names

    private List getAllTables() {
        List tableNames = new ArrayList<>();
        Session session = entityManager.unwrap(Session.class);
        SessionFactory sessionFactory = session.getSessionFactory();
        Map  map = (Map) sessionFactory.getAllClassMetadata();
        for(String entityName : map.keySet()){
            SessionFactoryImpl sfImpl = (SessionFactoryImpl) sessionFactory;
            String tableName = ((AbstractEntityPersister)sfImpl.getEntityPersister(entityName)).getTableName();
            tableNames.add(tableName);
        }
        return tableNames;
    }
    

提交回复
热议问题