Java – how to copy “show tables” in Hibernate?

I'm trying to iterate over all my tables, so I can truncate each table (at the beginning of each jbehave test)

I thought I could:

List<String> allTables = session.createsqlQuery("SHOW TABLES").list();

However, hibernate will throw an sqlgrammarexception, complaining that "the column 'table_name' cannot be found."

I think this is because the "show tables" query does not actually return a list of strings Is there any other way to use hibernate to get a list of all tables?

Solution

Try something like this:

SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE();

For columns (the same as hibernate), try:

SELECT column_name FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=YOUR_TABLE_NAME
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>