java - Jpa返回對(duì)象必須是與Entity類(lèi)么?
問(wèn)題描述
@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類(lèi)相關(guān)(Entity就是配置了Java類(lèi)與數(shù)據(jù)庫(kù)映射的Java類(lèi))有大神知道對(duì)么?
問(wèn)題解答
回答1:你這里報(bào)的錯(cuò)是查詢(xún)語(yǔ)句返回了一個(gè)Object[]數(shù)組,Jpa嘗試轉(zhuǎn)換成你自定義的對(duì)象,但是失敗了,可以試試以下的方式:
使用select new +對(duì)象全類(lèi)名 的語(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ù)組來(lái)接收數(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. docker鏡像push報(bào)錯(cuò)2. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””3. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題4. docker - 如何修改運(yùn)行中容器的配置5. javascript - 請(qǐng)指條明路,angular的$event,在select中卻是undefined?6. java - 如何點(diǎn)擊按鈕,重新運(yùn)行(我是初學(xué)者)?7. html5和Flash對(duì)抗是什么情況?8. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????9. javascript - 在 model里定義的 引用表模型時(shí),model為undefined。10. phpstudy8.1沒(méi)集成mysql-front

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