Reputation: 41
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.
Upvotes: 3
Views: 3955
Reputation: 5492
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".
Upvotes: 4