在jpa中获取最后一条记录的数据

问题描述 投票:0回答:1

我使用spring数据jpa和hibernate实现和postgres我有两个表

台式机:id,名称 表记录:id,machine_id,temp1,time_stamp

我搜索每台机器显示最后一条记录(id,machine_id,name,temp1)

这个SQL查询似乎可以完成这项工作

select r.*, d.*
from machine d
join record r on (d.id=r.machine_id)
left outer join record r2 on (d.id=tr2.machine_id and
(r.time_stamp<r2.time_stamp or r.time_stamp=r2.time_stamp and  r.id<r2.id)
)
where r2.id is null

但是我在jpa中搜索它

postgresql jpa spring-data-jpa
1个回答
0
投票

我猜你在机器和记录实体之间有一个双向的OneToOne关系。

select machine
from Machine machine
inner join machine.record record
left join machine.record record2 

where r2.id IS NULL

and (machine = record2.machine and record.time_stamp < record2.time_stamp or record.time_stamp = recrod2.time_stamp and record.id < record2.id)
© www.soinside.com 2019 - 2024. All rights reserved.