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.
-
Link: stackoverflow.com/a/3584161/259889 This helped I believe.Sid– Sid2015-01-05 07:10:26 +00:00Commented Jan 5, 2015 at 7:10
-
stackoverflow.com/questions/4813122/…Nikunj– Nikunj2015-01-05 07:10:52 +00:00Commented Jan 5, 2015 at 7:10
-
If entities are used, instead of native queries, Is there any approach to get the table names irrespective of the database?Sandeep– Sandeep2015-01-05 08:05:02 +00:00Commented Jan 5, 2015 at 8:05
-
Use the JDBC API: docs.oracle.com/javase/7/docs/api/java/sql/…user330315– user3303152015-01-05 08:14:45 +00:00Commented Jan 5, 2015 at 8:14
Add a comment
|
1 Answer
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".