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 管理的實(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
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題2. javascript - 求賜教:網(wǎng)易郵箱Web端模擬登錄看信的加密參數(shù)_ntes_nnid、_ntes_nuid3. javascript - 使用angular 的ui-sref 中出現(xiàn)了中文參數(shù),點(diǎn)擊跳轉(zhuǎn)后瀏覽器的地址欄里出現(xiàn)轉(zhuǎn)義后的%AE....%a%44. java - ConcurrentHashMap中的get()方法為什么可以不加鎖?5. javascript - QWebEngineView 如何爬 angular 的動態(tài)數(shù)據(jù)?6. html5 - 這個代碼顯示功能如何實(shí)現(xiàn)?7. javascript - 用JS 七牛上傳圖片出現(xiàn)文件已存在的錯誤(file exists)8. 工作近5年,3年Java Web ,近2年前端,未來何去何從?9. css3 - 圖片等比例縮放10. java - 字節(jié)流轉(zhuǎn)成字符串之后,在通過字符串轉(zhuǎn)成字節(jié)流后的文件為什么會不一樣?
