列出所有数据库表-JPA

时间:2018-12-03 11:07:16

标签: oracle spring-boot jpa

我想使用Spring Boot和JPA列出数据库中的所有表,我已经创建了DataSource的配置,如-configuring-spring-boot-for-oracle,并尝试了-

@Repository
public interface  TestRepo extends JpaRepository<Table, Long>{
    @Query("SELECT owner, table_name  FROM dba_tables")
    List<Table> findAllDB();
}

还有我的Table实体-

@Entity
public class Table {
    String owner;
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

}

得到-

No identifier specified for entity: com.siemens.plm.it.aws.connect.repos.Table

那么如何查询数据库表名称? 到目前为止,我的主要-

@SpringBootApplication
public class AwsFileUploadApplication implements CommandLineRunner{

    @Autowired
    DataSource dataSource;
    @Autowired
    TestRepo repo;

    public static void main(String[] args) {
        //https://wwwtest.plm.automation.siemens.com/subsadmin/app/products
        SpringApplication.run(AwsFileUploadApplication.class, args);

    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("DATASOURCE = " + dataSource); //some value - ds init sucess
        List<Table> findAllDB = repo.findAllDB();
        System.out.println(findAllDB);
        }
    }

从表中删除@Entity时-Not a managed type: class com.siemens.plm.it.aws.connect.repos.Table

1 个答案:

答案 0 :(得分:1)

我使用JDBC打印所有表和列:

    @Autowired
    protected DataSource dataSource;

    public void showTables() throws Exception {
        DatabaseMetaData metaData = dataSource.getConnection().getMetaData();
        ResultSet tables = metaData.getTables(null, null, null, new String[] { "TABLE" });
        while (tables.next()) {
            String tableName=tables.getString("TABLE_NAME");
            System.out.println(tableName);
            ResultSet columns = metaData.getColumns(null,  null,  tableName, "%");
            while (columns.next()) {
                String columnName=columns.getString("COLUMN_NAME");
                System.out.println("\t" + columnName);
            }
        }
    }