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. node.js - mysql如何通過knex查詢今天和七天內的匯總數據2. mysql 插入數值到特定的列一直失敗3. 360瀏覽器與IE瀏覽器有何區別???4. Python從URL中提取域名5. mysql - 百萬行的表中是否盡量避免使用update等sql語句?6. python - 在使用Pycharm時經常看到如下的樣式,小括號里紅色的部分是什么意思呢?7. javascript - 新浪微博網頁版的字數限制是怎么做的8. 怎么在網頁中設置圖片進行左右滑動9. javascript - 豆瓣的這個自適應是怎么做的?10. javascript - 用jsonp抓取qq音樂總是說回調函數沒有定義
