Home > Software engineering >  Query did not return a unique result
Query did not return a unique result

Time:01-06

This is the query I have written:

select c.id, CONCAT(c.major_version, '.', c.minor_version) as versions
from event_ids c
where c_id in ('101') group by c_id, major_version, minor_version;

This is the output I am getting from the database:

id versions
101 0.0
101 1.0
101 2.0
101 3.0

In my application, I am storing this result in

Map<List<String>, List<String>>

but it gives an error saying "query did not return a unique result"

How can I store this result? which data structure could I use?

CodePudding user response:

You should store your result in something like this:

Map<String, HashSet<String>>

The key is the id and the list contains the versions.

When you iterate the result set, you should

  1. Check if the key exists.
  2. If not, add it with an empty hashset of strings
  3. Add the version in the hashset
  4. Keep adding versions

CodePudding user response:

Try the below method:

void test2(ResultSet resultSet) throws SQLException {
        HashMap<String, List<String>> stringListHashMap = new HashMap<>();
        while (resultSet.next()){
            String id = resultSet.getString("id");
            String version = resultSet.getString("versions");
            if (stringListHashMap.containsKey(id)){ // check if the id is already available in the map 
                stringListHashMap.get(id).add(version); //if id is available get the list and add to the current version
            } else {
             // if not available create a new list 
             // add the current version to the list
             // then put the id and list to the map
                    List<String> versions = new ArrayList<>();
                    versions.add(version);
                    stringListHashMap.put(id,versions);
                }
            }
        }
  •  Tags:  
  • Related