java-AS400 db2上使用JPA的“未知列”
作者:互联网
我收到以下错误:
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 queryCaused by: org.hibernate.exception.SQLGrammarException: could not
execute queryCaused 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
解决方法:
好的,所以错误是我必须为上面的每一列选择值.
这意味着我这样查询:
@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)
并且有效.
标签:spring-data-jpa,jpa,java 来源: https://codeday.me/bug/20191111/2018485.html