JPA Select latest instance for each item

后端 未结 5 716
悲&欢浪女
悲&欢浪女 2020-12-03 05:09

Let\'s say I have a Meeting entity. Each meeting has a single attendee and a meeting date. Within my meeting table I may have multiple meetings for each attendee, with dif

5条回答
  •  無奈伤痛
    2020-12-03 05:25

    Well in SQL that would be quite simple I think, so I assume that can be mapped to JPA:

    SELECT m.AttendeeId, MAX(m.MeetingDate) from Meeting m GROUP BY m.AttendeeId
    

    Edit: If you need the messageId itself as well you can do that with a simple subquery that returns the messageId for a message where the other two values are equal. Just make sure you handle the case where there are several messageIds for the same Attendee and Date (eg pick the first result since they should all be equally good - although I'd doubt that such data even makes sense for meetings)

提交回复
热议问题