在 JPA 中选择具有 1 个条件的每个用户的最近日期的行
我有这个实体,我想为每个设备列出属性消息 = 1 的最后一个事件
@Entity
@Table(name = "t_device_event")
@NamedQueries(value = {
@NamedQuery(name = "DeviceEvent.findWithMessageActive",
query = "from DeviceEvent as de1 where de1.message = 1 and de1.received = "
+ " ( select max(de2.received) from DeviceEvent de2 "
+ " where de2.device.id = de1.device.id ) "), )
public class DeviceEvent {
..
}
但是我在上一个测试中有一个断言问题,因为它被认为是最后一个事件,而不是事实。device3
assertTrue ((numDevicesWithActiveMessage+1) == deviceEventService.findWithActiveMessage().size());
DeviceEvent deviceEvent3 = newDeviceEvent();
deviceEvent3.setMessage(1);
deviceEventService.save(deviceEvent3);
DeviceEvent deviceEvent4 = newDeviceEvent();
deviceEventService.save(deviceEvent4);
assertTrue ((numDevicesWithActiveMessage+1) == deviceEventService.findWithActiveMessage().size());