Getting all mapped Entities from EntityManager
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());
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);
}