whi*_*cat 2 java sql hibernate spring-data-jpa spring-boot
我正在使用 @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]
Run Code Online (Sandbox Code Playgroud)
我正在存储库中编写我的自定义查询。
发票仓库.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);
}
Run Code Online (Sandbox Code Playgroud)
报表服务.java
if(report.getReportBy().equals("invoiceCount")){
result = invoiceRepository.findInvoiceCount(report.getAgingDate());
}
Run Code Online (Sandbox Code Playgroud)
发票.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;
}
Run Code Online (Sandbox Code Playgroud)
数据库
它在将结果集映射到 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);
Run Code Online (Sandbox Code Playgroud)
spring data 从查询结果中获取结果集,并尝试将其映射到 Invoice 字段中(尝试构建 Invoice 类)。但实际类型是 Object[]。
如果你需要得到一些 DTO 作为结果你的查询,像结果集这样的字段:'c.id,c.business_name,count(i.id)' 使用@SqlResultSetMapping(你可以将结果列从选择查询映射到你的 dto) . 或者将返回类型从 List 更改为 Object[] 并根据需要对其进行迭代。
| 归档时间: |
|
| 查看次数: |
6216 次 |
| 最近记录: |