SpringJpa:如何获取具有唯一字段或第一个字段的对象列表

编程入门 行业动态 更新时间:2024-10-27 04:27:22
本文介绍了SpringJpa:如何获取具有唯一字段或第一个字段的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为 Message 的实体类:

I have an Entity class titled Message:

@Entity @Table(name = "Message") public class Message extends DefaultEntity { @Valid @ManyToOne(targetEntity = User.class) @JoinColumn(name="sender") private User sender; @Valid @ManyToOne(targetEntity = User.class) @JoinColumn(name="receiver") private User receiver; @NotNull private String message; @Enumerated(EnumType.STRING) private MessageStatus messageStatus; private Boolean isRead; }

顺便说一下,我删除了 getter 和 setter,所以它不会那么长.

by the way i removed the getters and setters, so it wouldnt be so long.

我想检索 MessageStatus 为READ"且发件人为唯一/最后/最新的聊天列表(因为两个特定用户(发件人和收件人)之间会有很多记录),我想要根据创建日期进入数据库的最新记录.

I want to retrieve a list of chats where the MessageStatus is "READ" and the Sender is Unique/Last/Latest (because there would be a lot of records between two specific users(sender and receiver)), I want the latest record to enter the DB based on date created.

我试过了,但我被卡住了,因为我不知道如何让应用程序根据创建的日期获取单个发件人;

I tried this, but i am stuck as i dont know how to make the app get a single sender based on the Date created;

List<Message> findMessagesByMessageStatusAndReceiver(Enum.MessageStatus messageStatus, User Receiver);

所以现在我有一些消息实体像这样保存到我的数据库中;

so now i have some message enitity persisted into my database like this;

{date, sender, receiver} , {2/5/7, 1, 2}, {2/6/7, 1, 2}, {2/8/7, 1, 2}, {2/9/7, 1, 2} , {2/5/7, 4, 2}, {2/5/7, 5, 2} , {2/5/7, 10, 2}, {1/5/7, 10, 2}, {2/3/7, 10, 2}

................我想要一种情况,当同一发件人有多个记录时,我可以获取所有消息,但具有唯一记录,因此在此我们有多个具有相同发件人 ID 的记录的情况:{2/5/7, 1, 2}, {2/6/7, 1, 2}, {2/8/7, 1, 2},{2/9/7, 1, 2} 仅根据最近的日期选择一个并添加到列表中,因此选择 {2/9/7, 1, 2} 以及此 {2/5/7, 10, 2}, {1/5/7, 10, 2}, {2/3/7, 10, 2} 这个 {2/5/7, 10, 2} 被选中.所以在一天结束时,我的列表是 {2/9/7, 1, 2}, {2/5/7, 4, 2}, {2/5/7, 5, 2}, {2/5/7, 10, 2}

................i want a situation where i can get all the messages but with a unique record when there are more than one record with the same sender, so in this situation where we have more that one record with the same sender ID : {2/5/7, 1, 2}, {2/6/7, 1, 2}, {2/8/7, 1, 2}, {2/9/7, 1, 2} only one gets picked based on the recent date and gets added to the list, so {2/9/7, 1, 2} gets picked and also for this {2/5/7, 10, 2}, {1/5/7, 10, 2}, {2/3/7, 10, 2} this {2/5/7, 10, 2} is picked. so at the end of the day my list is {2/9/7, 1, 2},{2/5/7, 4, 2}, {2/5/7, 5, 2}, {2/5/7, 10, 2}

推荐答案

您可以使用两种查询方法,findFirstBy.. 和 findTopBy...这两个关键字用于限制查询的结果.

There are two query methods that you can use for this, the findFirstBy.. and findTopBy... These two keywords are used to limit the results of a query.

您可以查看参考 示例.

编辑

做一个这样的查询:

消息 findFirstBySenderOrderByDate(Sender sender)

通过它您可以找到发件人的最新记录.然后,您可以从外部循环所有发件人,并为每个发件人调用查询方法.

with which you find most recent record for a sender. Then, from outside you can loop all senders, and call the query method for each one.

更多推荐

SpringJpa:如何获取具有唯一字段或第一个字段的对象列表

本文发布于:2023-11-30 18:00:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650998.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   第一个   对象   列表   SpringJpa

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!