BB8
BB8

Reputation: 41

Hibernate - Get all table names in a Database

I am trying to get all the table names in a database(Oracle 11g) to dynamically generate checkboxes for each table in the UI. I have not mapped any of these tables in the .cfg.xml file.

I used the below code :

List<Object> list = sessionProd.createQuery("select table_name from user_tables").list();                               

for(Object l : list){
    System.out.println("L : " +l.toString());
}

But it errored as below : org.hibernate.hql.internal.ast.QuerySyntaxException: user_tables is not mapped [select table_name from user_tables]

Please let me know if there is any way to get all table names in Hibernate 4

Upvotes: 0

Views: 8275

Answers (5)

v.ladynev
v.ladynev

Reputation: 19956

You need to use SQL query, not HQL query

sessionProd.createSQLQuery("select table_name from user_tables").list();

Upvotes: 4

BB8
BB8

Reputation: 41

Using a native SQL query method resolved the problem. Thanks for your suggestions.

The following code worked for me:

List<Object> list = sessionProd.createSQLQuery("select table_name from user_tables").list();     

Upvotes: 1

Dante
Dante

Reputation: 297

Hibernate would return Exception to you in such case because you have not mapped user_tables. If you want to get all table names you should to create SQLQuery, that would return to you that you need. You can use HQL (createQuery) only for mapped tables

Upvotes: 0

m.qadhavi
m.qadhavi

Reputation: 84

change the query string with select table_name from all_tables

List<Object> list = sessionProd.createQuery("select table_name from all_tables").list();                               

for(Object l : list){
     System.out.println("L : " +l.toString());
}

Upvotes: 0

Nasreen
Nasreen

Reputation: 112

I think the query is not proper. Try with the below snippet

List<Object> list = sessionProd.createQuery("show tables from Database_name").list();                               

Upvotes: 0

Related Questions