本文介绍了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:如何获取具有唯一字段或第一个字段的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:09