2017-06-22 78 views
1

我收到以下錯誤:「未知列」 與JPA在AS400 DB2

could not execute query; SQL [select DFBOGARM, sum(KVANTUM1) as KVANTUM1, (sum(DSTOMK) * 1000/sum(KVANTUM1)) as DSTOMK, (sum(DSTAKK) * 1000/sum(KVANTUMAKK)) as DSTAKK from NHODATA.ERHDSTV1 where DFSALVAR in(52, 55) group by DFBOGARM order by DFBOGARM]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

Caused by: org.hibernate.exception.SQLGrammarException: could not execute query

Caused by: com.ibm.as400.access.AS400JDBCSQLSyntaxErrorException: An undefined column name was detected.

查詢實現:

public interface DistributionRepository extends JpaRepository<ERHDSTV1, Long> { 

@Query(value="select " 
     + " DFBOGARM, " 
     + " sum(KVANTUM1) as KVANTUM1, " 
     + " (sum(DSTOMK) * 1000/sum(KVANTUM1)) as DSTOMK, " 
     + " (sum(DSTAKK) * 1000/sum(KVANTUMAKK)) as DSTAKK " 
     + "from NHODATA.ERHDSTV1 " 
     + "where DFSALVAR in(52, 55) " 
     + "group by DFBOGARM " 
     + "order by DFBOGARM", nativeQuery = true) 
List<ERHDSTV1> findSummedValuesFor52and55();} 

實體類:

@Entity 
@IdClass(ERHDSTV1Id.class) 
@Table(name = "ERHDSTV1") 
@Data 
public class ERHDSTV1 { 
    @Id 
    @Column(name = "DFBOGARM") 
    private String yearMonth; 
    @Id 
    @Column(name = "DFSALVAR") 
    private Long salesType; 
    @Column(name = "KVANTUM1") 
    private Long volumeOne; 
    @Column(name = "DSTOMK") 
    private Long distributionCosts; 
    @Column(name = "DSTMDN") 
    private Long distributionMedian; 
    @Column(name = "KVANTUMAKK") 
    private Long volumeAccumulated; 
    @Column(name = "DSTAKK") 
    private Long distributionAccumulated; 
    @Column(name = "DSTMNDAKK") 
    private Long distributionMedianAccumulated; 
} 

從查詢錯誤消息在從SQL工具執行時完美執行。

和標題有:DFBOGARM,KVANTUM1,DSTOMK,DSTAKK

+0

你的數據庫管理系統是否期望你的列名小寫?該消息是否指定哪個列名稱被稱爲未定義?你可以讓你的應用程序打印出實際生成的SQL嗎? – Nikolas

+0

已更新,但是,不,沒有提及具體的專欄,也不應該是大寫還是小寫。 –

+0

似乎錯誤表明SQL查詢是錯誤的,你有沒有試圖直接對你的數據庫運行它? – user3996996

回答

1

好了,這樣的錯誤是,我必須爲每列以上選擇的值。

這意味着我犯了這樣的查詢:

@Query(value = "select " 
     + " DFBOGARM, 1 as DFSALVAR, 1 as DSTMND, 1 as DSTMNDAKK, 1 as KVANTUMAKK, " 
     + " sum(KVANTUM1) as KVANTUM1, " 
     + " (sum(DSTOMK) * 1000/sum(KVANTUM1)) as DSTOMK, " 
     + " (sum(DSTAKK) * 1000/sum(KVANTUMAKK)) as DSTAKK " 
     + "from NHODATA.ERHDSTV1 " 
     + "where DFSALVAR in(52, 55) " 
     + "group by DFBOGARM " 
     + "order by DFBOGARM", nativeQuery = true) 

和工作。

+0

我遇到了與列大寫名稱相似的問題。這個答案有助於解決這個問題。 –