[英]sqlException : column not found JPA @query
I'm using @query annotation but when I try to fetch count of records it throws 我正在使用@query批注,但是当我尝试获取记录数时会抛出该异常
java.sql.SQLException: Column 'allowPartialPay' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094) ~[mysql-connector-java-5.1.31.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997) ~[mysql-connector-java-5.1.31.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983) ~[mysql-connector-java-5.1.31.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928) ~[mysql-connector-java-5.1.31.jar:na]
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1162) ~[mysql-connector-java-5.1.31.jar:na]
at com.mysql.jdbc.ResultSetImpl.getBoolean(ResultSetImpl.java:1781) ~[mysql-connector-java-5.1.31.jar:na]
I'm writing my custom queries in repository. 我在存储库中编写我的自定义查询。
InvoiceRepository.java InvoiceRepository.java
public interface InvoiceRepository extends JpaRepository<Invoice, Integer>{
Invoice findByInvoiceNumber(String invoiceNumber);
List<Invoice> findByUserId(int id);
@Query(value = "select c.id,c.business_name,count(i.id) from client c join invoice i on c.id = i.client_id where i.date <= :agingDate group by c.id",nativeQuery=true)
List<Invoice> findInvoiceCount(@Param("agingDate")Date agingDate);
}
ReportService.java ReportService.java
if(report.getReportBy().equals("invoiceCount")){
result = invoiceRepository.findInvoiceCount(report.getAgingDate());
}
Invoice.java 发票.java
@Entity
@Table
public class Invoice {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@ManyToOne
private Client client;
@Column
private boolean allowPartialPay;
}
Database 数据库
It comes during mapping result set into java Invoice class (as you declared it as return type List for method findInvoiceCount() ). 它在将结果集映射到java Invoice类的过程中出现(正如您将其声明为方法findInvoiceCount()的返回类型List一样)。 native query return in your case Object[] instead of List.
本机查询在您的情况下返回Object []而不是List。
You can see it in log exception 您可以在日志异常中看到它
ResultSetImpl.findColumn(ResultSetImpl.java:1162
ResultSetImpl.findColumn(ResultSetImpl.java:1162
So it happens in result mapping stage ,after query has executed. 因此,它发生在查询执行后的结果映射阶段。
@Query(value = "select c.id,c.business_name,count(i.id) from client
c join invoice i on c.id = i.client_id
where i.date <= :agingDate group by c.id",nativeQuery=true)
List<Invoice> findInvoiceCount(@Param("agingDate")Date agingDate);
spring data gets result set from query result and tries to map it into Invoice field by field (try to contruct Invoice class ). spring数据从查询结果中获取结果集,并尝试将其逐字段映射到Invoice(尝试构造Invoice类)。 But actual type it's Object[].
但是实际类型是Object []。
If you need get some DTO as result your query , with fields like is result set : 'c.id,c.business_name,count(i.id)' use @SqlResultSetMapping (you can map result columns from select query into your dto). 如果您需要在查询中获取一些DTO作为结果,其结果字段为:'c.id,c.business_name,count(i.id)',请使用@SqlResultSetMapping(您可以将选择查询的结果列映射到dto中) 。 Or change return type from List to Object[] and iterate it as you need.
或将返回类型从List更改为Object []并根据需要对其进行迭代。
Here is example for Result Set Mapping: The Basics . 这是结果集映射的示例:基础知识 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.