定义列表的顺序 [英] Defining the order of a list

查看:405
本文介绍了定义列表的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我有三个类,A,B和C. A包含一个关于B:s的OneToMany关系列表。 B包含与C的ManyToOne关系.C包含名为name的字段,B还包含名为name的字段。我想要完成的是将A列表中的项目主要按C的名称排序,其次按B的名称排序 - 问题是我不知道如何做到这一点。它甚至可能吗?

I have the following problem. I have three classes, A, B and C. A contains a OneToMany relationed list of B:s. B contains a ManyToOne relation to C. C contains a field called "name" and B also contains a field called "name". What I'd like to accomplish is to have the items in A's list sorted primarily by C's name and secondarily by B's name - the problem is that I do not know how to do this. Is it even possible?

我正在使用EclipseLink作为我的JPA提供商。

I'm using EclipseLink as my JPA provider.


class A {
   @OneToMany
   @OrderBy("b.c.name, b.name") <---- this is the problem
   List<B> b;
}

class B {
   @ManyToOne
   C c;
   String name;
}

class C {
   String name;
}

编辑
是的,我尝试了不同的变体,例如@OrderBy(c.name)不起作用,我只是收到一条错误消息,告诉我实体类b不包含名为c.name的字段。

EDIT Yes, I've tried different variations, for example @OrderBy("c.name") doesn't work, I just get an error message telling me that the entity class b does not contain a field called "c.name".

推荐答案

这是不可能的。 @OrderBy只接受直接属性/字段名称,而不接受嵌套属性。这是有道理的,真的,因为c表 - 取决于你的提取策略可能甚至不是为了检索你的b而发出的选择的一部分。

It's NOT possible. @OrderBy only accepts direct property / field names, not nested properties. Which makes sense, really, because "c" table - depending on your fetching strategy may not even be part of a select issued to retrieve your "b"s.

这篇关于定义列表的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆