使用hibernate获取db中的所有表 [英] Get all Tables in a Db using hibernate

查看:430
本文介绍了使用hibernate获取db中的所有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用hibernate检索数据库中所有表的名字?
我在oracle的db中执行了查询 SELECT TABLE_NAME FROM USER_TABLES ,它工作得很好。
但是当谈到DB2时,它不会。

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.

推荐答案

您可以使用

You can use

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

这将返回所有持久化实体(感谢HQL隐式多态性),这与数据库无关。请注意,它将排除没有记录的表格。

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.

如果你需要所有的表格,包括空的表格,你可以使用原生的sql查询

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();

本地查询的缺点是它对每个数据库都是特定的,例如,在Oracle上查询是select * from user_tables。

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

这篇关于使用hibernate获取db中的所有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆