[ACCEPTED]-Getting all mapped Entities from EntityManager-persistence
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 1 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 7 all of the mapped entities and their corresponding 6 SQL tables (there may be others).
The most 5 straightfoward is if you can use your Hibernate 4 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 3 can do a little more casting and get this 2 same information out of the SessionFactory 1 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);
}
as MarcG answer states the getAllClassMetadata()
is deprecated 2 since few years ago
As of Hibernate - 5.4.30.Final - released March 19th, 2021 The code below works 1 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)));
}
}
Check what's available in this Map
:
((Session) entityManager.getDelegate()).getSessionFactory().getAllClassMetadata() ;
0
If you have javax.persistence.EntityManager 2 in hand., following method can help you 1 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;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.