How can I replicate “SHOW TABLES” in Hibernate?

前端 未结 3 611
感动是毒
感动是毒 2021-01-14 05:14

I\'m trying to iterate over all of my tables so I can truncate each one (at the beginning of each of my JBehave tests).

I thought I would be able to:



        
3条回答
  •  旧巷少年郎
    2021-01-14 06:20

    If you still have access to the Hibernate Configuration object, you can do this:

    for (Iterator iter=configuration.getClassMappings(); iter.hasNext();) {
      PersistentClass persistentClass = (PersistentClass)iter.next();
      String table = persistentClass.getTable().getName();
      // Code to truncate table (or just use a query with session.executeUpdate)
    }
    

    That assumes you have one table per entity and you only care about the tables that are mapped. Otherwise, you probably need to do something with the underlying connection and the DatabaseMetaData, e.g.:

    session.connection().getMetaData().getTables(catalog, schemaPattern, tableNamePattern, types)
    

提交回复
热议问题