JPQL 中的复杂排序依据

发布于 2024-10-01 19:08:14 字数 2145 浏览 2 评论 0原文

首先我要说的是我不是一个数据库专家。这些东西简直让我困惑死了,但不幸的是我在工作中被迫这样做,所以我有点卡住了:-)

我试图通过在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜清冷一曲。 2024-10-08 19:08:14

您有多种选择:

从 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

用心笑 2024-10-08 19:08:14

如果您唯一的问题是对结果列表进行排序,那么一种方法是在全局字段上添加@OrderBy(默认顺序是ASC,可以省略):

@OrderBy("YOUR_COLUMN_NAME")

如果您想要降序排列,语法将是:

@OrderBy("YOUR_COLUMN_NAME DESC")

例如:

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "TASK_ID")
@OrderBy("TASK_ID") // <-------- ASC
private Task task;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SRC_ID")
@OrderBy("SRC_ID DESC") // <-------- DESC
private CommonSource src;

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):

@OrderBy("YOUR_COLUMN_NAME")

If you want descending order, the syntax will be:

@OrderBy("YOUR_COLUMN_NAME DESC")

For example:

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "TASK_ID")
@OrderBy("TASK_ID") // <-------- ASC
private Task task;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SRC_ID")
@OrderBy("SRC_ID DESC") // <-------- DESC
private CommonSource src;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文