![](/img/trans.png)
[英]Spring Data JPA: @Query gives error java.sql.SQLException: Column 'id' not found
[英]sqlException : column not found JPA @query
我正在使用@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());
}
發票.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;
}
數據庫
它在將結果集映射到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類)。 但是實際類型是Object []。
如果您需要在查詢中獲取一些DTO作為結果,其結果字段為:'c.id,c.business_name,count(i.id)',請使用@SqlResultSetMapping(您可以將選擇查詢的結果列映射到dto中) 。 或將返回類型從List更改為Object []並根據需要對其進行迭代。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.