2
I need help with the case of converting the Hibernate SQL result into json.
I reached the stage that is converting correct, but the same does not come with the defined name of the column, and I was needing the result to come in the json with the name of the respective column.
SQL:
select locationApiAcp as acp from Pro
Function:
public List<Map<String,Object>> listByNativeQuery(String query) {
Query nativeQuery = manager.createNativeQuery(query);
return nativeQuery.getResultList();
}
Calling for:
List<Map<String,Object>> listObj = local._runLibrary(cq);
Gson gs = new Gson();
String json = gs.toJson(listObj);
System.out.println(json);
Final result not exiting with column name:
["/api/"]
---Edit--- A small solution that I found using a parallel connection to JPA. I don’t know if it would be the right one or could do with the jpa’s own connection.
Class for later 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;
}}
Processing of the Resultset:
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);
}
Utluiz, I don’t know if it would be the case, but I ended up solving it with a parallel connection to what I’ve been using with Hibernate. I edited my post with what I got.
– Kelvin Stang Oenning
@Kelvinstangoenning The solution with JDBC looks good. Can post it as answer to your own question is to accept it as sure.
– utluiz
I can no longer add an answer because the topic appears to be pending by the administration, but I put it there as edited. In the case there after the "-Edit---" I am processing the Resultset in hand.
– Kelvin Stang Oenning