Get all tables in Db using hibernate

Is there a way to get the name of all tables in the database using hibernate? I ran a query SELECT TABLE_NAME FROM USER_TABLES

in oracle Db and it works fine. But when it comes to DB2, this is not the case.

+3


source to share


1 answer


you can use

List<Object> list = session.createQuery("from java.lang.Object").list();

      

This will return all persistent objects (thanks to HQL implicit polymorphism) and this is db independent. Note that it excludes tables with no records.



If you need all tables, including empty tables, you can use your own SQL query

List<Object[]> list = session.createSQLQuery("select * from sysibm.systables").list();

      

The disadvantage to a native query is that it is specific to each database, for example in Oracle the query is "select * from user_tables".

+4


source







All Articles