Try this code if you are using oracle and want to query table names in database. I am not sure if it works with other databases as well.
Code:
String sql = "select table_name from all_tables where owner='YOUR_DB_SCHEMA_NAME'";
Query q = session.createSQLQuery( sql );
List names = q.list();
if ( names != null && !(names.isEmpty()) ) {
int length = names.size();
for ( int i=0; i<length; i++ ) {
Object object = names.get( i );
System.out.println("object = " + object.toString());
}
}
or else try this code if you want to query configured tables with Hibernate SessionFactory
Code:
SessionFactory factory =HibernateUtil.getSessionFactory();
Map metadata = factory.getAllClassMetadata();
Iterator it = metadata.entrySet().iterator();
while ( it.hasNext() ) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
}