Data Retrieval is not in the given order?
greetings all i a开发者_开发百科m using spring framework and hibernate to communicate with database i have a table that contain some records suppose with ids 1,2,3,4,5 when i tried to make an HQL query to retrieve the data ordered by id asc data is retrieved in a correct order,but when trying to loop on the data with enhanced for loop, the order is reversed, i don't know why ???????
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
when making for loop on them the order is reversed
for (MyDTO myObj : data) {
System.out.println("Id: " + myObj.getEntityId());
}
// prints 5,4,3,2,1
any ideas why such behavior occurs ?
asc
is the default order, so you can omit that.
Otherwise it should work as expected. Make sure you are iterating the list properly.
精彩评论