Reputation: 3984
I want to list all the tables in my DB using Spring boot and JPA
, I have created a DataSource
configuration like - configuring-spring-boot-for-oracle and tried -
@Repository
public interface TestRepo extends JpaRepository<Table, Long>{
@Query("SELECT owner, table_name FROM dba_tables")
List<Table> findAllDB();
}
And my Table
Entity -
@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;
}
}
And got -
No identifier specified for entity: com.siemens.plm.it.aws.connect.repos.Table
So how do I query for DB tables names? So far my main -
@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);
}
}
When removing @Entity from Table - Not a managed type: class com.siemens.plm.it.aws.connect.repos.Table
.
Upvotes: 3
Views: 9284
Reputation: 7950
I print all my tables and columns using 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);
}
}
}
If you look at the getTables API you can see how to refine the search of tables.
Upvotes: 13