Conversion from hibernate result to json [closed]

2

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);

}
    
asked by anonymous 14.12.2016 / 18:33

1 answer

3

I did a little test with the code:

List<Map<String,Object>> listObj = new ArrayList<>();
Map<String,Object> map1 = new HashMap<>();
map1.put("f1", "v1");
map1.put("f2", 1L);
listObj.add(map1);
Map<String,Object> map2 = new HashMap<>();
map2.put("f1", 1.2D);
listObj.add(map2);

Gson gs = new Gson();
String json = gs.toJson(listObj);
System.out.println(json);

And I got the output:

  

Therefore, the serialization code correctly.

Something important to understand is that the generic type of the list (in this case: Map<String,Object> ) is just a compile-time check, not a run-time guarantee.

In this case, the method getResultList does not informs the generic type. In reality, in general, when there is no JPA entity selected, Hibernate returns an array containing the field values in the order in which they were selected in the query. Since you have selected only one field, hibernate is probably just returning a list where each element is a string with the value of the field.

So what's wrong with the code is that it assumes the wrong type in each list item resulting from the query.

If you need to return values such as a map, or any other structure, you need to rearrange the values in the list so that it is compatible with the structure you want for JSON.

When doing a query like this, inspect via debugging which values Hibernate actually returns and never assume typing without confirming before, after all native queries do not have the assurances of mapping the JPA entities.

    
15.12.2016 / 02:00