JPQL 中的复杂排序依据
首先我要说的是我不是一个数据库专家。这些东西简直让我困惑死了,但不幸的是我在工作中被迫这样做,所以我有点卡住了:-)
我试图通过在 JPQL 中工作来获得一个复杂的订单,但我没有任何一点运气都没有。
我当前的查询如下所示:
select distinct msg from CSMessage msg, Error err where msg = err.msg order by err.task.src
我试图完成的任务是获取所有具有相关错误的消息,然后使用源文档(err.task.src)对整个内容进行排序,以获取所有错误消息同一来源一起出现。
不用说,这根本不起作用。我得到一个例外: “ORDER BY 项应位于 SELECT DISTINCT 列表中”
我查看了文档和其他来源,似乎没有任何内容可以帮助我。
有人能指出我正确的方向吗?
谢谢
编辑 1:
实体如下所示:
短信消息
public class CSMessage extends BaseModel implements Serializable { private static final long serialVersionUID = 1L; . . . Other fields not shown for brevity . . . @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "TASK_ID") private Task task; }
错误:
public class Error { private static final long serialVersionUID = 1L; @Column(name = "ERR_STRING", length = 255) private String errString; @Column(name = "ERR_TYPE") private Integer errType; @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "MSG_ID") private CSMessage msg; @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "SRC_ID") private CommonSource src; @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "TASK_ID") private Task task; }
任务
public class Task { private static final long serialVersionUID = 1L; @Column(name = "CORRELATION_UUID", length = 36) private String correlationId; @Column(name = "CURRENT_NODE", length = 255) private String currentNodeName = "empty"; @Column(name = "PROCESS_NAME", length = 255) private String processName = "empty"; @Column(name = "SITE_ID", length = 10) private String siteId = "1"; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "SRC_ID") private CommonSource src; }
Let me start by saying I'm not a DB guy. This stuff just confuses me to death, but unfortunately I got roped into doing this at work, so I'm kinda stuck :-)
I'm trying to get a complex order by to work in JPQL, and I'm not having any luck at all.
My current query looks like this:
select distinct msg from CSMessage msg, Error err where msg = err.msg order by err.task.src
What I'm trying to accomplish with this is to get all the msgs's with related errors, then sort the whole thing using the source document (err.task.src), to get all errored messages with the same source to appear together.
Needless to say this doesn't work at all. I get an exception that says; "ORDER BY item should be in the SELECT DISTINCT list"
I've looked over the docs and other sources and there doesn't seem to be anything in there that can help me.
Can anyone point me in the right direction?
Thanks
Edit 1:
The entities look like this:
CSMessage
public class CSMessage extends BaseModel implements Serializable { private static final long serialVersionUID = 1L; . . . Other fields not shown for brevity . . . @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "TASK_ID") private Task task; }
Error:
public class Error { private static final long serialVersionUID = 1L; @Column(name = "ERR_STRING", length = 255) private String errString; @Column(name = "ERR_TYPE") private Integer errType; @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "MSG_ID") private CSMessage msg; @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "SRC_ID") private CommonSource src; @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "TASK_ID") private Task task; }
Task
public class Task { private static final long serialVersionUID = 1L; @Column(name = "CORRELATION_UUID", length = 36) private String correlationId; @Column(name = "CURRENT_NODE", length = 255) private String currentNodeName = "empty"; @Column(name = "PROCESS_NAME", length = 255) private String processName = "empty"; @Column(name = "SITE_ID", length = 10) private String siteId = "1"; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "SRC_ID") private CommonSource src; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有多种选择:
从 CSMessage msg 中选择不同的 msg、Error err where msg = err.msg order by err.task.src
将 err.task.src 添加到 select 子句 (select msg, err.task.src) 和更改方法中的返回类型
使用接口 Comparable (实现 Comparable 并使用 Collections.sort)
您也可以使用 criteriaQuery 或 nativeQuery
You have several options:
select distinct msg from CSMessage msg, Error err where msg = err.msg order by err.task.src
Add err.task.src to select clause (select msg, err.task.src) and change return type in your method
Order in memory instead of bd, using interface Comparable (implements Comparable and use Collections.sort)
Also you could use criteriaQuery or nativeQuery
如果您唯一的问题是对结果列表进行排序,那么一种方法是在全局字段上添加@OrderBy(默认顺序是ASC,可以省略):
如果您想要降序排列,语法将是:
例如:
If your only problem is to order the result list, then one way to do it is to add @OrderBy on the global fields (default order is ASC which can be left out):
If you want descending order, the syntax will be:
For example: