From query i wil get 2 rows but while returing the map i am not getting 2 rows i am getting only last row

huangapple go评论94阅读模式
英文:

From query i wil get 2 rows but while returing the map i am not getting 2 rows i am getting only last row

问题

// 在这个方法中,我通过查询从数据库获取数据,我得到了2行数据,但在返回映射时,我只得到了最后一行

public Map<Integer, String> getname(int id) {

  1. Session session = sessionFactory.openSession();
  2. // 创建映射
  3. Map<Integer, String> stylesMap = new LinkedHashMap<Integer, String>();
  4. try {
  5. Query query = getSession().createSQLQuery("select distinct id,name from employee where id=" + id);
  6. List results = query.list();
  7. Iterator itr = results.iterator();
  8. // 遍历值
  9. while (itr.hasNext()) {
  10. Object row1[] = (Object[]) itr.next();
  11. // 将值设置到映射中
  12. stylesMap.put(row1[0] == null ? 0 : Integer.parseInt(row1[0].toString()), (row1[1] == null) ? "" : row1[1].toString());
  13. }
  14. } catch (Exception e) {
  15. logger.error("", e);
  16. } finally {
  17. if (session != null) {
  18. session.close();
  19. }
  20. }
  21. // 返回映射
  22. return stylesMap;

}

英文:

// i created on method in that i am getting data from database through query , i am getting 2 rows but while returning the map i am not getting 2 rows it returning only last row

  1. public Map&lt;Integer, String&gt; getname(int id) {
  2. Session session = sessionFactory.openSession();
  3. //creating map
  4. Map&lt;Integer,String&gt; stylesMap = new LinkedHashMap &lt;Integer,String&gt;();
  5. try {
  6. Query query=getSession().createSQLQuery(&quot;select distinct id,name from employee where id=&quot;+id);
  7. List results = query.list();
  8. Iterator itr = results.iterator();
  9. //iterating the values
  10. while (itr.hasNext()) {
  11. Object row1[] = (Object[]) itr.next();
  12. //setting values into map
  13. stylesMap.put(row1[0]==null?0:Integer.parseInt(row1[0].toString()), (row1[1] == null)? &quot;&quot; : row1[1].toString());
  14. }
  15. }catch (Exception e) {
  16. logger.error(&quot;&quot;, e);
  17. } finally{
  18. if(session !=null) {
  19. session.close();
  20. }
  21. }
  22. // returning map
  23. return stylesMap;

}

答案1

得分: 0

你的数据库返回了两行数据:

  1. 7 1
  2. 7 2

你试图将这些信息存储在一个映射中,但由于映射只能为一个键存储一个值,第一个值将被覆盖。

如果你的数据库包含两个相同ID的记录(理想情况下不应该出现这种情况),那么你可以将这些信息存储在以下方式之一:

  1. 一个 pairs 列表,类似于 List&lt;Pair&lt;int,int&gt;&gt;

  2. 或者你可以创建一个 EmployeeDTO

    1. public class EmployeeDTO{
    2. String id;
    3. String name;
    4. }

    然后你可以将你得到的结果存储在一个 List&lt;EmployeeDTO&gt; 中。

英文:

Your DB returns two rows with data

  1. 7 1
  2. 7 2

You are trying to store this information in a map, but since a map stores only one value for a key, the first one will be overwritten.

If your DB contains two records for the same id( which ideally should not be the case), then you can store this information in

  1. A list of pairs, something like List&lt;Pair&lt;int,int&gt;&gt;

  2. Or you can create an EmployeeDTO

    1. public class EmployeeDTO{
    2. String id;
    3. String name;
    4. }

    And you can store the results you got in a List&lt;EmployeeDTO&gt;

huangapple
  • 本文由 发表于 2020年8月9日 20:11:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63326203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定