![](/img/trans.png)
[英]javax.persistence.NonUniqueResultException: query did not return a unique result
[英]Query did not return a unique result
这是我写的查询:
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;
这是我从数据库中获取的 output:
ID | 版本 |
---|---|
101 | 0.0 |
101 | 1.0 |
101 | 2.0 |
101 | 3.0 |
在我的应用程序中,我将此结果存储在
Map<List<String>, List<String>>
但它给出了一个错误,说“查询没有返回唯一的结果”
我怎样才能存储这个结果? 我可以使用哪种数据结构?
您应该将结果存储在如下内容中:
Map<String, HashSet<String>>
关键是 id,列表包含版本。
当你迭代结果集时,你应该
试试下面的方法:
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.