gpt4 book ai didi

java - 使用 JPA 对集合的集合进行分组和排序

转载 作者:行者123 更新时间:2023-12-01 20:12:00 25 4
gpt4 key购买 nike

我正在寻找一个 REST Controller ,它将返回各种对象的排序列表。

我创建了一个 DTO 来保存这些集合,如下所示,但这不起作用,因为它将按实体分组:

public class AllReportsDTO {

private List<AReport> aReports;
private List<BReport> bReports;
private List<CReport> cReports;
...
}

然后我有以下域对象

@Entity
@Table(name = "a_report")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AReport implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "title")
private String title;

@Column(name = "description")
private String description;

@Column(name = "time_of_report")
private ZonedDateTime timeOfReport;

每份报告一份。

我想要做的是创建一个端点,它将返回所有这些报告的列表,但按报告的时间顺序排列,而不是按报告分组。我怎样才能实现这个目标?

我尝试使用 HQL 查询将其写入存储库并按时间分组,但问题是每个时间字段在每个报告中都有不同的名称,由于该系统在其他地方使用,因此我无法更改该名称。

最佳答案

您可以创建对集合进行排序的方法。尝试熟练掌握这个

Collections.sort( aReports, new Comparator<Object>() {
public int compare(MyObject o1, Object o2) {
return o1.getTimeOfReport().compareTo(o2.getTimeOfReport());
}
});

关于java - 使用 JPA 对集合的集合进行分组和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46561191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com