Saturday, December 27, 2014

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();
}
References:
  • https://stackoverflow.com/questions/4813122/get-all-table-names-set-up-in-sessionfactory
  • http://www.herongyang.com/JDBC/sqljdbc-jar-Table-List.html
  • http://tutorials.jenkov.com/jdbc/databasemetadata.html

7 comments:

chotu darling said...

thanks for the post

HAPPY VALENTINES DAY

Unknown said...

Java is the most robust secured and multi threaded programming language which is the reason why most the the developers go for java. A single java code can be used for various platforms.
JAVA training in chennai | java training institutes in chennai | FITA Academy Chennai

Raji said...

Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
Devops training in Chennai | Devops training Institute in Chennai

karthick said...

Good to read very impressive
Data Science Training in chennai

ranjitham kannan said...

It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
Datascience Course in Chennai | Datascience Training in Chennai

Mithun said...

Thanks for all your valuable Articles.Here Looking for JAVA Training with Placements Visit Here...
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

laxmicynixit said...

nice information thanks for sharing...........!
ui path training