我有一個按每日日期計算行數的SQL截斷(月,日,小時)。考慮歷史圖。如果在pgAdmin中執行,查詢工作正常,但使用EclipseLink在Java中失敗。PostgreSQL查詢在pgAdmin中工作,但在Java中不起作用EclipseLink
pgAdmin的查詢:
SELECT date_trunc('hour', delivered_at),
COUNT(date_trunc('hour', delivered_at)) AS num
FROM messages
WHERE channel_type='EMAIL'
AND created_at>='2016-02-28 16:01:08.882'
AND created_at<='2016-02-29 16:01:08.882'
GROUP BY date_trunc('hour', delivered_at);
JPQL的命名查詢
SELECT FUNCTION('date_trunc', 'hour', m.deliveredAt),
COUNT(FUNCTION('date_trunc', 'hour', m.deliveredAt)) AS num
FROM Message m
WHERE m.channelType = :channelType
AND m.createdAt >= :fromDate
AND m.createdAt <= :toDate
GROUP BY FUNCTION('date_trunc', 'hour', m.deliveredAt)
EclipseLink的調試日誌:
SELECT date_trunc(?, delivered_at), COUNT(date_trunc(?, delivered_at)) FROM messages
WHERE (((channel_type = ?) AND (created_at >= ?)) AND (created_at <= ?)) GROUP BY date_trunc(?, delivered_at)
bind => [hour, hour, EMAIL, 2015-12-27 00:00:00.0, 2015-12-27 00:00:00.0, hour]
錯誤:
ERROR: column "messages.delivered_at" must appear in the GROUP BY clause or be used in an aggregate function Position: 23
PostgreSQL的日誌:
2016-03-01 13:22:08 CET ERROR: column "messages.delivered_at" must appear in the GROUP BY clause or be used in an aggregate function at character 23 2016-03-01 13:22:08 CET STATEMENT: SELECT date_trunc($1, delivered_at), COUNT(delivered_at) FROM messages WHERE (((channel_type = $2) AND (created_at >= $3)) AND (created_at <= $4)) GROUP BY date_trunc($5, delivered_at) 2016-03-01 13:22:08 CET LOG: execute S_2: SELECT 1
如果我取代從EclipseLink的綁定的變量記錄查詢,並pgAdmin的執行它的查詢工作。這裏發生了什麼?
編輯:只是澄清,它也可以使用em.createNativeQuery。
嘗試使用查詢提示關閉參數綁定,並查看Postgres是否仍然存在語句問題:https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Query_Hints#Bind_Parameters否則,請嘗試另一個JDBC驅動程序。 – Chris
我試着用真假,沒有改變。你是什麼意思不同的JDBC dirver?我使用postgresql jdbc42驅動程序9.4。 – cen
除了內聯參數之外,根據日誌,本機SQL查詢與EclipseLink執行的內容之間有什麼區別?將eclipselink.jdbc.bind-parameters屬性設置爲false時執行的查詢是什麼 - 它與您傳遞給createNativeQuery的本機SQL相匹配嗎? – Chris