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, 再發(fā)表一次看看那}’; 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類相關(guān)(Entity就是配置了Java類與數(shù)據(jù)庫映射的Java類)有大神知道對么?
問題解答
回答1:你這里報的錯是查詢語句返回了一個Object[]數(shù)組,Jpa嘗試轉(zhuǎn)換成你自定義的對象,但是失敗了,可以試試以下的方式:
使用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[]數(shù)組來接收數(shù)據(jù) ,Object[]中的每一個元素值就是對應(yīng)列的值
@Query(select p.id,p.name,p.age from Person p) List<Object[]> findPersonResult();
先查出Person ,用java代碼轉(zhuǎn)換成PersonResult
相關(guān)文章:
1. css3 - rem布局下,用戶瀏覽器的最小字號是12px怎么辦?2. javascript - 循環(huán)嵌套多個promise應(yīng)該如何實現(xiàn)?3. mysql優(yōu)化 - 關(guān)于mysql分區(qū)4. css - 移動端字體設(shè)置問題5. html5 - 如何實現(xiàn)帶陰影的不規(guī)則容器?6. vue.js - vue 打包后 nginx 服務(wù)端API請求跨域問題無法解決。7. javascript - ionic2 input autofocus 電腦成功,iOS手機(jī)鍵盤不彈出8. node.js - 在vuejs-templates/webpack中dev-server.js里為什么要exports readyPromise?9. 前端 - IE9 css兼容問題10. objective-c - iOS開發(fā)支付寶和微信支付完成為什么跳轉(zhuǎn)到了之前開發(fā)的一個app?
