Ask Your Question
2

How can I set values on a Map using more than one parameter from a nativeQuery?

asked 2023-02-01 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-05-08 07:00:00 +0000

qstack gravatar image

You can iterate over the results of the native query and set the values on the map using multiple parameters. Here's an example:

Map<String, Integer> map = new HashMap<>();

Query query = entityManager.createNativeQuery("SELECT name, score FROM students");

List<Object[]> results = query.getResultList();

for (Object[] result : results) {
    String name = (String) result[0];
    Integer score = (Integer) result[1];
    map.put(name, score);
}

In the above example, we create a native query to retrieve the name and score of all students. We then execute the query and iterate over the results. For each result, we extract the name and score and set them as key-value pairs in the map.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-01 11:00:00 +0000

Seen: 13 times

Last updated: May 08 '22