java - Jpa返回對象必須是與Entity類么?
問題描述
@Query(value = 'SELECT id as topicId,content FROM bbs_topic WHERE create_time BETWEEN ?1 AND ?2',nativeQuery = true) List<IndexObject> getBbsTopicListByDate(Date fileupdateDate, Date topiclastupdate);
其中IndexObject 是顯示層vo。然后報錯
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [com.wayne.common.lucene.entity.IndexObject] for value ’{59, 再發表一次看看那}’; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [com.wayne.common.lucene.entity.IndexObject]
度娘了一下懷疑 jpa返回對象必須是與Entity類相關(Entity就是配置了Java類與數據庫映射的Java類)有大神知道對么?
問題解答
回答1:你這里報的錯是查詢語句返回了一個Object[]數組,Jpa嘗試轉換成你自定義的對象,但是失敗了,可以試試以下的方式:
使用select new +對象全類名 的語法, 此處的Perso 為EntityManager 管理的實體,PersonResult為自定義的實體
@Query(select new com.xx.yy.PersonResult(p.id,p.name,p.age) from Person p) List<PersonResult> findPersonResult();
使用Object[]數組來接收數據 ,Object[]中的每一個元素值就是對應列的值
@Query(select p.id,p.name,p.age from Person p) List<Object[]> findPersonResult();
先查出Person ,用java代碼轉換成PersonResult
相關文章:
1. java - spring boot 如何打包成asp.net core 那種獨立應用?2. java - 在用戶不登錄的情況下,用戶如何添加保存到購物車?3. javascript - webpack 分割加載代碼后,react 界面不更新4. 安全性測試 - nodejs中如何防mySQL注入5. html - eclipse 標簽錯誤6. javascript - nginx反向代理靜態資源403錯誤?7. python文檔怎么查看?8. javascript - 關于apply()與call()的問題9. datetime - Python如何獲取當前時間10. android - SwipeRefreshLayout5.0以下不兼容
