orderBy 和 OneToMany-Relation -- 使用 jpa 2.0 进行条件查询
我有以下结构...
Entity A
- ...
- Collection<B> c (@OneToMany)
Entity B
- ...
- Date d
我希望 queryResult 通过 criteriaQuery 中的 orderBy-Statement 按日期 d 排序。我怎样才能实现这个目标?
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<A> cq = cb.createQuery(A.class);
Root<A> r = cq.from(A.class);
cq.select(r);
// orderBy-Statement -> cq.orderBy(cb.desc(r.get("c.d")).as(Date.class)));
// Error: javax.servlet.ServletException: javax.ejb.EJBException: java.lang.IllegalArgumentException: Unable to resolve attribute [c.d] against path
List<A> l = em.createQuery(cq).getResultList();
是否可以在查询中进行排序? 如果没有,我应该编写一个方法来排序我的 resultList,对吧?
I have the following structure...
Entity A
- ...
- Collection<B> c (@OneToMany)
Entity B
- ...
- Date d
I want the queryResult to be ordered by the Date d via an orderBy-Statement in my criteriaQuery. How can I achieve this?
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<A> cq = cb.createQuery(A.class);
Root<A> r = cq.from(A.class);
cq.select(r);
// orderBy-Statement -> cq.orderBy(cb.desc(r.get("c.d")).as(Date.class)));
// Error: javax.servlet.ServletException: javax.ejb.EJBException: java.lang.IllegalArgumentException: Unable to resolve attribute [c.d] against path
List<A> l = em.createQuery(cq).getResultList();
Is it possible to have the ordering in the query?
If not, i'm supposed to write a method for ordering my resultList afterwards, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,总而言之,我无法弄清楚如何使用我想要的 orderBy-Statement。但现在我对另一种方法感到满意(在我看来这是一个更好的方法!):
顺便说一句:我正在使用 Primefaces.org 框架。省去了我很多工作^^
Well, all in all I wasn't able to figure out how to use the orderBy-Statement I want. But Now I'm satisfied with an alternative approach (and in my opinion an even better one!):
By the way: I'm using the Primefaces.org-framework. Saves me a lot of work^^
这个呢?
What about this one?