[英]Error 'No converter found capable of converting from type XXX' when using @Query("Select * from")
我有一個類DocumentData
:
@Data //Lombok
public class DocumentData {
private Long documentId;
private Long binderId;
private String containerCode;
private String barcode;
private Long clientId;
}
我有我的存儲庫:
public interface DocumentArchiveRepository extends JpaRepository<DocumentArchive, Long> {
String QUERY_GET_DOCUMENT_DATA = "SELECT DISTINCT d.id_document, pd.id_binder, s.container_code, d.barcode, k.id_client FROM table1 pd \n"
+ "JOIN table2 d ON pd.id_document = d.id_cokument\n"
+ "JOIN table3 s ON s.id = pd.id_binder \n"
+ "JOIN table4 k ON k.id_document = d.id_document\n"
+ "WHERE pd.id_position = 10122 AND d.id_document in :documentIds";
@Query(value = QUERY_GET_DOCUMENT_DATA, nativeQuery = true)
List<DocumentData> getDocumentData(@Param("documentIds") List<Long> documentIds);
當我想獲取數據時:
List<DocumentData> documentData = repository.getDocumentData(documentIds);
我收到以下錯誤:
org.springframework.core.convert.ConverterNotFoundException:找不到能夠從類型 [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] 轉換為類型 [pl.ultimo.retention.archive.dto 的轉換器。 DocumentData] 在 org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] 在 org.springframework.core。 convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE] 在 org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.convert(GenericConversionService) .java:174) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
我應該將代碼更改為List<Object>
並將結果每年映射到DocumentData
嗎? 或者也許有更好的解決方案?
您必須將查詢包裝在構造函數中:
String QUERY_GET_DOCUMENT_DATA = "SELECT new " + DocumentData.class.getName() + "(d.id_document, pd.id_binder, s.container_code, d.barcode, k.id_client) FROM table1 pd "
+ "JOIN table2 d ON pd.id_document = d.id_cokument "
+ "JOIN table3 s ON s.id = pd.id_binder "
+ "JOIN table4 k ON k.id_document = d.id_document "
+ "WHERE pd.id_position = 10122 AND d.id_document in :documentIds";
注意大括號。 此外,我正在使用DocumentData.class.getName()
,但如果它在類路徑上是唯一的,則只需使用名稱"DocumentData"
。 我不確定這是否可以作為本機查詢運行,請嘗試一下,如果不能 - 作為 jpql 運行。
其他解決方案是定義自定義Converter
實現並使用 @Converter 注釋@Converter
。
最后,我沒有為我的案例找到合適的解決方案,其中有許多沒有實體的聯接。 我必須創建所有必要的實體,只有需要的字段,然后在我的查詢中使用它們的名稱(使用 Select new MyPOJOClass(...))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.