对 JPQL 实体列表进行排序
我花了很长时间努力寻找答案,但到目前为止还没有任何运气。我正在尝试通过 JPQL 查询检索和排序对象列表,但由于查询本身使用不同表之间的大量联接,因此这确实很困难。
基本上,我们有一个实体“Person”,其中“电话”、“电子邮件”和“地址”字段
String name
List<Telephone> phones
List<Email> emails
List<Address> addresses
都是单独的实体,每个实体都包含自己的数据,例如字符串字段“数字”或类似的字段(Address.street,地址.状态)。所以这四个对象都是数据库中的表。
我希望用户能够按任何特定数据对人员列表进行排序。现在,我想按以下任意一项进行排序:人员姓名、电子邮件列表中的第一个电子邮件地址、人员第一个地址所在的街道或州等。因此,如果人员列表如下:
Name Phone Number Street State
Mack 555-1234 1 Main Street WA
Andy 222-9999 2 Other Way RI
Wendy 222-3333 3 Wrong Way UT
并且我希望该表按州排序,则列表应该是:
Name Phone Number Street State
Andy 222-9999 2 Other Way RI
Wendy 222-3333 3 Wrong Way UT
Mack 555-1234 1 Main Street WA
我希望使用 JPQL 查询来完成此操作,以便列表在提供给Web 服务器以优化性能。我还必须补充一点,我正在实现一个搜索功能,可以在所有这些“列”中搜索特定术语。
长话短说,如何编写 JPQL 查询,以便可以获得已针对这些列之一进行排序的 Person 对象列表?我的 "SELECT DISTINCT": 实现
SELECT DISTINCT x FROM Person x, IN(x.phones) phones ORDER BY phones.number
不起作用,因为结果表需要对列进行排序(这是无效的,因为它导致列表不符合 Person 对象列表的资格),而且我似乎无法使用嵌套的 SELECT 语句生成结果集,然后从中提取 Person 对象的列表。有什么简单的方法可以做到这一点吗?
I've searched long and hard for an answer, but I haven't had any luck so far. I'm trying to retrieve and sort a list of objects via a JPQL query, but since the query itself uses a lot of joins between different tables, it's really difficult.
Basically, we have an entity "Person" with the fields
String name
List<Telephone> phones
List<Email> emails
List<Address> addresses
"Telephone", "Email", and "Address" are all separate Entities, each containing its own data, like a String field "number" or something similar (Address.street, Address.state). So all four of these objects are tables in the database.
I want the user to be able to sort a list of Persons by any particular data. Right now, I want to sort by any one of the following: the Person's name, the first email address in the emails list, by the street or state of the person's first address, and so on. So if a list of Persons is of the following:
Name Phone Number Street State
Mack 555-1234 1 Main Street WA
Andy 222-9999 2 Other Way RI
Wendy 222-3333 3 Wrong Way UT
and I want the table to be sorted by State, the list should be:
Name Phone Number Street State
Andy 222-9999 2 Other Way RI
Wendy 222-3333 3 Wrong Way UT
Mack 555-1234 1 Main Street WA
I want this to be done using a JPQL query so a list is already filtered and sorted when it's given to the web server to optimize performance. I must also add that I am implementing a search feature that searches all these "columns" for a particular term.
Long story short, how do I write a JPQL query such that a can obtain a list of Person objects that have been sorted for one of these columns? My implementation of "SELECT DISTINCT":
SELECT DISTINCT x FROM Person x, IN(x.phones) phones ORDER BY phones.number
doesn't work since the result table requires to have the column being sorted (which is invalid because it causes the list to not qualify as a list of Person objects), and I can't seem to use nested SELECT statements to generate the result set and then pull a list of Person objects from that. Is there any easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,但我必须不同意。在 JPQL 中,您可以(!)对结果集进行排序,而无需在结果中包含已排序的列。
查看任何 Hibernate 示例(作为突出的 JPQL 实现)。
如果我错过理解,请告诉我
I am sorry but I must disagree. In JPQL you can (!) sort the result set without including the sorted column in the result.
See any Hibernate example (as a promonent JPQL implementation).
Please let me know if I miss understood