lukaszrys
lukaszrys

Reputation: 1786

How to add results to java hashmap when using hibernate sql with join?

I couldn't find the proper question for my issue, so i came up only with that one. So i want to put my results from my query question to the hashmap, my question is sth like this

import org.hibernate.Query;
private static final String SELECT_MANUFACTURER_PROJECT_DISP_ID = "SELECT O.manufacturers, P.dispid" +
        "FROM offerOrder O" +
        "JOIN offerOrderProjectRel R" +
        "ON O.id = R.offerOrderId" +
        "JOIN project P" +
        "ON P.id = R.projectId" +
        "WHERE O.type = 'ORDER'";
public Map <String,String >createManufacturerProjectDispIdMap() {
    Map<String, String> map = new LinkedHashMap<String, String>();
    Query q = getSession().createQuery(SELECT_MANUFACTURER_PROJECT_DISP_ID);
    return map;
}

As you can see im connecting with 3 tables but i think it doesnt matter. I'm getting O.manufacturer and P.dispid correctly, but i dont know how to put it into hashmap like this

O.manufacturer1 -> P.dispid1
O.manufacturer2 -> P.dispid2
O.manufacturer3 -> P.dispid3
[...]

This is my first time with hibernate so probably thats why i have this issue and i still dont know how it works probably. Sorry for my english and thx for an answer in advance. It doesnt have to be Map<String,String> so feel free to give me answer with any map

Upvotes: 0

Views: 1701

Answers (1)

Sachin Thapa
Sachin Thapa

Reputation: 3709

You can interate over the result and put yourself into a map something like this:

Query q = getSession().createQuery(SELECT_MANUFACTURER_PROJECT_DISP_ID);
for(Object o : q.list())
{
   map.put(o.id(), o);
}

Cheers !!

Upvotes: 1

Related Questions