This post demonstrates the how to get the all table names using Hibernate.
Configuration configuration = new Configuration();References:
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();
}
- 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:
thanks for the post
HAPPY VALENTINES DAY
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
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
Good to read very impressive
Data Science Training in chennai
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
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
nice information thanks for sharing...........!
ui path training
Post a Comment