java - Jpa返回對(duì)象必須是與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。然后報(bào)錯(cuò)
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返回對(duì)象必須是與Entity類相關(guān)(Entity就是配置了Java類與數(shù)據(jù)庫(kù)映射的Java類)有大神知道對(duì)么?
問題解答
回答1:你這里報(bào)的錯(cuò)是查詢語(yǔ)句返回了一個(gè)Object[]數(shù)組,Jpa嘗試轉(zhuǎn)換成你自定義的對(duì)象,但是失敗了,可以試試以下的方式:
使用select new +對(duì)象全類名 的語(yǔ)法, 此處的Perso 為EntityManager 管理的實(shí)體,PersonResult為自定義的實(shí)體
@Query(select new com.xx.yy.PersonResult(p.id,p.name,p.age) from Person p) List<PersonResult> findPersonResult();
使用Object[]數(shù)組來接收數(shù)據(jù) ,Object[]中的每一個(gè)元素值就是對(duì)應(yīng)列的值
@Query(select p.id,p.name,p.age from Person p) List<Object[]> findPersonResult();
先查出Person ,用java代碼轉(zhuǎn)換成PersonResult
相關(guān)文章:
1. mac里的docker如何命令行開啟呢?2. 為什么我ping不通我的docker容器呢???3. nignx - docker內(nèi)nginx 80端口被占用4. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””5. docker容器呢SSH為什么連不通呢?6. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題7. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。8. javascript - 最近用echarts做統(tǒng)計(jì)圖時(shí)遇到兩個(gè)問題!!9. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????10. docker gitlab 如何git clone?

網(wǎng)公網(wǎng)安備