简体   繁体   English

sqlException:找不到列JPA @query

[英]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.

相关问题 Spring Data JPA:@Query给出错误java.sql.SQLException:找不到列“ id” - Spring Data JPA: @Query gives error java.sql.SQLException: Column 'id' not found SQLException:未找到列 - SQLException: Column not found 奇怪的SQLException:找不到列 - Strange SQLException: Column not found 带有SqlResultSetMapping的JPA Sql NativeQuery抛出java.sql.SQLException:找不到列&#39;formula3_0_&#39; - JPA Sql NativeQuery with SqlResultSetMapping throwing java.sql.SQLException: Column 'formula3_0_' not found JDBC:“SQLException:找不到列名” - JDBC: “SQLException:Column name is not found” Spring Data JPA查询给出java.sql.SQLException:无效的列类型错误 - Spring Data JPA Query gives java.sql.SQLException: Invalid column type error JPA 查询对不在查询中的列抛出“未找到”错误 - JPA query is throwing a 'not found' error for column that isn't in query java.sql.SQLException: 列 'n_id_proyecto_categoria_has_proyecto' 未找到,查询 hibernate - java.sql.SQLException: Column 'n_id_proyecto_categoria_has_proyecto' not found, query hibernate Informix java.sql.SQLException:在查询的任何表中找不到列(…)(或SLV未定义) - Informix java.sql.SQLException: Column (…) not found in any table in the query (or SLV is undefined) java.sql.SQLException:找不到列错误? - java.sql.SQLException: Column not found Error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM