I am using JPA on top of App Engine (I am quite new to both) and I am currently facing a behaviour I do not understand.

Each time I refresh the page, the order of the fetched items changes.

Here is the code snippet:

Set<Cast> results = new HashSet<Cast>();
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery(FIND_ALL_SORTED_BY_DESCENDING_BROADCAST_DATE);
@SuppressWarnings("unchecked")
List<Cast> casts = query.getResultList();
for (Cast cast : casts) {
    if (verifySecondaryFields(cast)) {
        results.add(synchronizeTechnicalFields(cast));
    }
}
entityManager.close();
return Collections.unmodifiableSet(results);

where FIND_ALL_SORTED_BY_DESCENDING_BROADCAST_DATE actually is SELECT cast FROM Cast cast ORDER BY cast.broadcastDate DESC.

entityManagerFactory is an autowired member of my repository class.

The thing is the ORDER BY clause seems to be ignored and the results show up randomly. Can you spot what is wrong?

有帮助吗?

解决方案

Sets don't preserve order. Lists do. Try List<Cast> = new ArrayList<Cast>(); and carry on from there.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top