How to return a MAP using JPA’s native Query?

Asked

Viewed 158 times

2

I need to consult a bank that is already established, populated and maintained by another application. So I don’t have the entities in my project and I didn’t want to create.

I am using Spring Boot with JPA. Below is an example of code:

@PersistenceContext
protected EntityManager manager;

public Map<String, Object> getUsuario(String email, String senha) {
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("email", email);
    parameters.put("senha", senha);

    Query query = manager.createNativeQuery(
            "SELECT * FROM usuarios WHERE email = :email AND senha = :senha");
    preencherParametros(parameters, query);

    List<Map<String, Object>> resultList = query.getResultList();

    return resultList.get(0);
}

I wanted the result of this query to return me a Map with the column name and its value. How to do this?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.