我正在使用@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]
我在存储库中编写自定义查询。
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
if(report.getReportBy().equals("invoiceCount")){
result = invoiceRepository.findInvoiceCount(report.getAgingDate());
}
Invoice.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;
}
数据库
答案 0 :(得分:1)
在将结果集映射到java Invoice类期间(因为您将其声明为方法findInvoiceCount()的返回类型List)。本机查询返回您的案例Object []而不是List。
您可以在日志异常中看到它
ResultSetImpl.findColumn(ResultSetImpl.java:1162
因此,在查询执行完毕后,它会在结果映射阶段发生。
@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数据从查询结果中获取结果集,并尝试按字段将其映射到Invoice字段(尝试构造Invoice类)。但实际输入它的对象[]。
如果你需要获得一些DTO作为结果你的查询,结果集如下:&#39; c.id,c.business_name,count(i.id)&#39;使用@SqlResultSetMapping(您可以将select查询中的结果列映射到您的dto中)。或者将返回类型从List更改为Object []并根据需要进行迭代。