728x90
How to get all table names using Hibernate.
This post demonstrates the how to get the all table names using Hibernate.
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
// Way1 - get the all Class Mapping Information using Configuration
Iterator<PersistentClass> classMappings = configuration.getClassMappings();
while(classMappings.hasNext()) {
PersistentClass persistentClass = classMappings.next();
Table table = persistentClass.getTable();
String tableName = table.getName();
System.out.println(tableName);
}
// Way2 - get the all Class Metadata Information using SessionFactory
Map<String, ClassMetadata> classMetaDataMap = sessionFactory.getAllClassMetadata();
for(Map.Entry<String, ClassMetadata> metaDataMap : classMetaDataMap.entrySet()) {
ClassMetadata classMetadata = metaDataMap.getValue();
AbstractEntityPersister abstractEntityPersister = (AbstractEntityPersister) classMetadata;
String tableName = abstractEntityPersister.getTableName();
System.out.println(tableName);
}
// Way3 - using Session
SessionImpl sessionImpl = (SessionImpl) sessionFactory.openSession();
try {
Connection connection = sessionImpl.connection();
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[] {"TABLE"});
while(resultSet.next()) {
String tableName = resultSet.getString(3);
System.out.println(tableName);
}
} catch (Exception e) {
e.printStackTrace();
}
http://myjourneyonjava.blogspot.kr/2014/12/how-to-get-all-table-names-using.html
728x90
'Hibernate' 카테고리의 다른 글
hibernate Could not find setter (모델 ProjectionList 오류) (1) | 2016.06.02 |
---|---|
select, save update 시 디비 특정 function을 호출해야될경우 (0) | 2016.04.06 |
hibernate criteria query 조건문 (0) | 2016.02.23 |
onetomany order by 정렬 순서 문제 (0) | 2016.02.23 |