久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

java - Jpa返回對象必須是與Entity類么?

瀏覽:140日期:2023-12-20 18:30:58

問題描述

@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 管理的實(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[]中的每一個元素值就是對應(yīng)列的值

@Query(select p.id,p.name,p.age from Person p) List<Object[]> findPersonResult();

先查出Person ,用java代碼轉(zhuǎn)換成PersonResult

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 宁海县| 维西| 长乐市| 蕉岭县| 陇南市| 尤溪县| 宁波市| 墨竹工卡县| 彭水| 托里县| 永德县| 清远市| 潍坊市| 乌海市| 武威市| 鄱阳县| 出国| 德化县| 民乐县| 长乐市| 广宁县| 望都县| 会泽县| 余姚市| 繁昌县| 黎平县| 西乡县| 淮安市| 莒南县| 阿克陶县| 灯塔市| 叶城县| 马公市| 时尚| 陵水| 丰镇市| 渭源县| 水城县| 荣昌县| 日照市| 阜宁县|