3

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.

4

1 Answer 1

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".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.