JpaRepository 方法名称与 DISTINCT ON

use*_*048 5 spring-data-jpa

我正在创建 Spring 应用程序,并且正在使用 JpaRepository 来简化数据库查询。

我在使用 DISTINCT ON 的 JpaRepository 约定创建方法名称时遇到问题。这是我的 SQL 查询:

SELECT DISTINCT ON (s.device_id)
  s.status_id,
  s.error,
  s.receive_time,
  s.device_id
FROM statuses s
ORDER BY s.device_id, s.receive_time DESC
Run Code Online (Sandbox Code Playgroud)

我试过这样的名字,但它不起作用:

List<Status> findDistinctByDeviceId_OrderByReceiveTimeDesc();
Run Code Online (Sandbox Code Playgroud)

这是我的课程(简化版):

public class Status {
    private long status_id;
    private String error;
    private Date receive_time;
    private Device device_id;
}

public class Device {
    private long device_id;
    private String name;
}
Run Code Online (Sandbox Code Playgroud)

甚至可以在方法名称中使用 DISTINCT ON 吗?我不想使用 SQL 查询,因为我的类比上面的复杂得多,我想将每个字段添加到查询中。

use*_*048 5

我使用了这样的东西并且它有效:

@Query(value = "select distinct on (s.device_id) s.* " +
        "from statuses s " +
        "order by s.device_id, s.receive_time desc",
        nativeQuery = true)
public List<Status> getStatuses();
Run Code Online (Sandbox Code Playgroud)