[Vuejs]-How to have keys in my Json, not only values

0👍

The Json you need is actually not json. here is what you actually want :

[
   {
    "id":32,
    "fullName":"DE BELLOUET Jag",
    "acs":"1.3.13.3.",
    "nom_service":"Cid polseruti sau"
   },
   {
    "id":15,
    "fullName":"NOURAUD Benjamin",
    "acs":"1.3.13.3.",
    "nom_service":"Cid polseruti sau"
   }
]

the important detail is the { and } characters that are used to denoted an Object or a map of properties.

With that said you should probably create a POJO to store the result of your SQL request. maybe something like :

public class Agent {
    private Integer id;
    private String fullName;
    private String acs;
    private String nomService;

    // add some getters and setter here

}

Maybe returning a List<Agent> from your rest method could be enough. If not you could take a look at the jackson library to serialize a List<Agent> into json.

0👍

The first part of the solution actually involves creating a class (AgentServ).

But hibernate doesn’t like my request, and it had to have the results in a list of Object[].

Then we had to cast the previous results, Include the lines in an agent object one step at a time and then add it to the list.

The datatable now returns the requested result.

Below is the final code.

public List<AgentServ> listAgentService() {
    String buf = null;
    buf = "SELECT a.id AS id,(btrim(a.nom) || ' ' || btrim(a.prenom)) AS fullName, s.acs AS acs, s.libelle AS nom_service ";
    buf += "FROM Agent a INNER JOIN Service s ON a.service = s ";
    buf += "WHERE ((a.actif = true) OR ((a.actif = false) AND (a.dateSuppression >= CURRENT_DATE)))";

    List<Object[]> query = em.createQuery(buf.toString(), Object[].class).getResultList();
    List<AgentServ> agents = new ArrayList<AgentServ>();
    
    for(Object[] row : query) {
        AgentServ agent = new AgentServ();
        agent.setId((long) row[0]);
        agent.setFullName((String) row[1]);
        agent.setAcs((String) row[2]);
        agent.setNom_service((String) row[3]);
        
        agents.add(agent);
    }
    
    return agents;      
}

Leave a comment