Google App Engine / Java - ORDER BY 不一致
我在 App Engine 之上使用 JPA(我对两者都很陌生),目前我面临着一种我不理解的行为。
每次刷新页面时,获取的项目的顺序都会发生变化。
以下是代码片段:
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);
其中 FIND_ALL_SORTED_BY_DESCENDING_BROADCAST_DATE
实际上是 SELECT cast FROM Cast cast ORDER BY cast.broadcastDate DESC
。
entityManagerFactory
是我的存储库类的自动装配成员。
问题是 ORDER BY 子句似乎被忽略,结果随机显示。你能发现哪里出了问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
集合不保持顺序。列表可以。尝试
List= new ArrayList();
并从那里继续。Sets don't preserve order. Lists do. Try
List<Cast> = new ArrayList<Cast>();
and carry on from there.