![](/img/trans.png)
[英]Creating date_trunc index via Hibernate 4.3.5 / JPA 2.1
[英]Springboot Hibernate & JPA and Native Queries (Group by PostgreSQL date_trunc) pageable problem
我在 SpringBoot 1.4.7 中遇到了這個 JPA 查詢。 問題是我在嘗試處理本機查詢時收到此錯誤。 我已經測試了 SQL 查詢並且都是有效的。
org.springframework.dao.InvalidDataAccessApiUsageException: Could not locate named parameter [signals], expecting one of [toDate, from, device]; nested exception is java.lang.IllegalArgumentException: Could not locate named parameter [signals], expecting one of [toDate, from, device]
查詢定義為:
@Query(value = "select device, signal, avg(avg), min(min), max(max), date_trunc('day', timestamp) as timestamp from devicestatistics d "
+ "where timestamp between :from and :toDate and device like :device and signal IN (:signals) "
+ "group by d.device ,d.signal ,date_trunc('day' , d.timestamp ) order by device,date_trunc('day' ,d.timestamp) Desc \\n-- #pageable\\n",
countQuery = "SELECT count(1) as NumRows from "
+ "(select device, signal, avg(avg), min(min), max(max), date_trunc('day', :from) as timestamp from devicestatistics d "
+ "where 'time' between :from and :toDate and device like :device "
+ "group by device ,signal ,date_trunc('day' ,d.time ) order by device , date_trunc('day' ,d.time ), signal x"
,nativeQuery = true)
Page<DeviceStatistics> getDataDay(@Param("from") Date from, @Param("toDate") Date toDate, @Param("device") String device, @Param("signals") Collection<String> signals,Pageable page);
該表如下(我知道名稱選擇不明智)。
CREATE TABLE public.devicestatistics (
device text NOT NULL,
"TIMESTAMP" timestamp NOT NULL,
signal text NOT NULL,
avg float8 NULL,
max int8 NULL,
min int8 NULL,
CONSTRAINT devicestatistics_pkey PRIMARY KEY (device, "TIMESTAMP", signal)
);
實體定義為:
@Getter
@Setter
@Entity
@Table(name = "DEVICESTATISTICS")
@IdClass(DeviceStatisticsID.class)
public class DeviceStatistics implements Serializable {
@Id
@Column(name = "DEVICE")
private String device;
@Id
@Column(name = "SIGNAL")
private String signal;
@Column(name = "AVG")
private double average;
@Column(name = "MIN")
private double minimun;
@Column(name = "MAX")
private double maximun;
@Id
@Column(name = "TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date time;
有什么提示嗎?
這個問題類似於這個問題,但我希望使用本機查詢 function。
謝謝!
編輯:
如果我停止使用 Pageable 它可以工作,但是我無法使用 Pageable 使其工作......(我一直在 stackoverflow 中搜索解決方案,但沒有工作)
@Query(value = "select device, signal, avg(avg), min(min), max(max), date_trunc('day', timestamp) as timestamp from devicestatistics d "
+ "where timestamp between :from and :toDate and device like :device and signal IN (:signals) "
+ "group by d.device ,d.signal ,date_trunc('day' , d.timestamp ) order by device,date_trunc('day' ,d.timestamp) Desc",
countQuery = "SELECT count(1) as NumRows from "
+ "(select device, signal, avg(avg), min(min), max(max), date_trunc('day', :from) as timestamp from devicestatistics d "
+ "where 'time' between :from and :toDate and device like :device "
+ "group by device ,signal ,date_trunc('day' ,d.time ) order by device , date_trunc('day' ,d.time ), signal x"
,nativeQuery = true)
List<DeviceStatistics> getDataDay(@Param("from") Date from, @Param("toDate") Date toDate, @Param("device") String device, @Param("signals") Collection<String> signals);
最后我得到了它。 如果有人需要通過分組進行這種原生查詢,這可能會有所幫助。
@Query(value = "select device, signal, avg(avg), min(min), max(max), date_trunc('day', timestamp) as timestamp from devicestatistics d "
+ "where timestamp between :from and :toDate and device like :device and signal IN (:signals) "
+ "group by d.device ,d.signal ,date_trunc('day' , d.timestamp ) order by device,date_trunc('day' ,d.timestamp) Desc --#pageable\n" ,
countQuery = "SELECT count(1) as NumRows from "
+ "(select device, signal, avg(avg), min(min), max(max), date_trunc('day', timestamp) as timestamp from devicestatistics d "
+ "where timestamp between :from and :toDate and device like :device and signal IN (:signals) "
+ "group by device ,signal ,date_trunc('day' ,d.timestamp ) order by device , date_trunc('day' ,d.timestamp ) Desc) x"
,nativeQuery = true)
Page<DeviceStatistics> getDataDayIn(@Param("from") Date from, @Param("toDate") Date toDate, @Param("device") String device, @Param("signals") Collection<String> signals, Pageable page);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.