简体   繁体   English

Hibernate - 获取数据库中的所有表名

[英]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.我正在尝试获取数据库(Oracle 11g)中的所有表名,以便为 UI 中的每个表动态生成复选框。 I have not mapped any of these tables in the .cfg.xml file.我没有在 .cfg.xml 文件中映射任何这些表。

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]但它错误如下: org.hibernate.hql.internal.ast.QuerySyntaxException: user_tables is not mapping [select table_name from user_tables]

Please let me know if there is any way to get all table names in Hibernate 4请让我知道是否有任何方法可以在 Hibernate 4 中获取所有表名

您需要使用 SQL 查询,而不是 HQL 查询

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

Using a native SQL query method resolved the problem.使用本机 SQL 查询方法解决了该问题。 Thanks for your suggestions.感谢您的建议。

The following code worked for me:以下代码对我有用:

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

I think the query is not proper.我认为查询不正确。 Try with the below snippet尝试使用以下代码段

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

change the query string with select table_name from all_tables使用 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());
}

Hibernate would return Exception to you in such case because you have not mapped user_tables.在这种情况下,Hibernate 会向您返回 Exception,因为您尚未映射 user_tables。 If you want to get all table names you should to create SQLQuery, that would return to you that you need.如果你想获得所有你应该创建 SQLQuery 的表名,那将返回给你你需要的。 You can use HQL (createQuery) only for mapped tables您只能对映射表使用 HQL (createQuery)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM