代码之家  ›  专栏  ›  技术社区  ›  Mahmoud Saleh

数据检索不按给定顺序进行?

  •  -1
  • Mahmoud Saleh  · 技术社区  · 14 年前

    大家好 我使用spring框架和hibernate与数据库通信 我有一个表,其中包含一些记录,假设id为1,2,3,4,5 当我试图进行HQL查询以检索按id asc排序的数据时 数据是以正确的顺序检索的,但是当尝试使用增强的for循环对数据进行循环时,顺序是相反的,我不知道为什么???????

    List<MyDTO> data = getCurrentSession()
                    .createQuery(
                            "from MyDTO where indicator=:indicator order by entityId")
                    .setLong("indicator", 10).list();
            System.out.println("First Id In The Query: "
                    + data.get(0).getEntityId()); // prints 1
    

    在它们上做循环时,顺序是相反的

    for (MyDTO myObj : data) {
    System.out.println("Id: " + myObj.getEntityId());
    }
    // prints 5,4,3,2,1
    

    你知道为什么会发生这种行为吗?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Bozho    14 年前

    asc 是默认顺序,因此可以忽略该顺序。

    否则它应该按预期工作。确保正确地迭代列表。