I need some help with the case of converting the hibernate SQL result to json.
SQL:
select locationApiAcp as acp from Pro
Function:
public List<Map<String,Object>> listByNativeQuery(String query) {
Query nativeQuery = manager.createNativeQuery(query);
return nativeQuery.getResultList();
}
Call:
List<Map<String,Object>> listObj = local._runLibrary(cq);
Gson gs = new Gson();
String json = gs.toJson(listObj);
System.out.println(json);
Final result not coming out with column name:
["/api/"]
--- Edit --- A small solution I found using a parallel connection to JPA. I do not know if it would be the correct one or would give to do with the connection of the own jpa.
Class for further conversion:
public class JsonApi {
private String status;
private List<HashMap<String, String>> rows = new ArrayList<>();
public void _addRow(HashMap<String, String> map) {
this.rows.add(map);
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<HashMap<String, String>> getRows() {
return rows;
}
public void setRows(List<HashMap<String, String>> rows) {
this.rows = rows;
}}
ResultSet Processing:
this.json.setStatus("200");
while (this.rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
HashMap<String, String> map = new HashMap<>();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String column = rsmd.getColumnLabel(i);
map.put(column, this.rs.getString(column));
}
this.json._addRow(map);
}