首先,为我的语法错误道歉。我的英语水平不好。
我正在尝试读取多个字段,这些字段将成为表中的列。但我不知道该怎么做。因为,我尝试使用来自查询的 getResultList() 循环。
我正在使用带有postgsql的spring boot(jpa + hibernate)。
这个想法是代替下一个代码:
public List<Object> readTable(String nameTable) {
String SQL_COLUMN_TABLE_ID = "SELECT table_id FROM " + nameTable + " ORDER BY table_id asc;";
String SQL_COLUMN_GEOM = "SELECT GeometryType(geom) FROM " + nameTable + " ORDER BY table_id asc;";
String SQL_COLUMN_PROPERTIES = "SELECT CAST(properties AS text) FROM " + nameTable + " ORDER BY table_id asc;";
List<String> table_id = executeSQLReadTable(SQL_COLUMN_TABLE_ID);
List<String> geom_type = executeSQLReadTable(SQL_COLUMN_GEOM);
List<String> properties = executeSQLReadTable(SQL_COLUMN_PROPERTIES);
List<Object> results = new ArrayList<>();
for (int i=0; i<table_id.size(); i++) {
List<Object> item = new ArrayList<>();
item.add(table_id.get(i));
item.add(geom_type.get(i));
item.add(properties.get(i));
results.add(item);
}
return results;
}
要使用这个:
public List<String> readPerfectTable(String nameTable) {
String SQL = "SELECT table_id, CAST(properties AS text), GeometryType(geom) FROM " + nameTable + " ORDER BY table_id asc;";
return executeSQLReadTable(SQL);
}
在这部分,我不知道如何使用select的多个字段的结果:
private List<String> executeSQLReadTable(String SQL) {
List<String> results = new ArrayList<>();
try {
Query query = em.createNativeQuery(SQL);
List<?> list = query.getResultList();
for (Object item : list) {
// Here WTF!
results.add(item.toString());
}
} catch(Throwable e) {
throw e;
} finally {
em.close();
}
return results;
}