Is there a way to retrieve the name of all tables in the database using hibernate?
I executed the query SELECT TABLE_NAME FROM USER_TABLES
in an oracle Db and it works just fine.
But when it comes to DB2, it wont.
Asked
Active
Viewed 3,743 times
3
-
Link: http://stackoverflow.com/a/3584161/259889 This helped I believe. – Sid Jan 05 '15 at 07:10
-
http://stackoverflow.com/questions/4813122/get-all-table-names-set-up-in-sessionfactory – Nikunj Jan 05 '15 at 07:10
-
If entities are used, instead of native queries, Is there any approach to get the table names irrespective of the database? – Sandeep Jan 05 '15 at 08:05
-
Use the JDBC API: http://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getTables%28java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String[]%29 – Jan 05 '15 at 08:14
1 Answers
4
You can use
List<Object> list = session.createQuery("from java.lang.Object").list();
This will return all persistent entities (thanks to HQL implicit polymorphism), and this is db independent. Note that it will exclude tables with no records.
If you need all tables, including the empty ones, you can use native sql query
List<Object[]> list = session.createSQLQuery("select * from sysibm.systables").list();
The drawback for the native query is that it is specific for each database, for example, on Oracle the query is "select * from user_tables".

outdev
- 5,249
- 3
- 21
- 38