您的位置:首頁技術文章
文章詳情頁

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

瀏覽:115日期: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, 再發表一次看看那}’; 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類相關(Entity就是配置了Java類與數據庫映射的Java類)有大神知道對么?

問題解答

回答1:

你這里報的錯是查詢語句返回了一個Object[]數組,Jpa嘗試轉換成你自定義的對象,但是失敗了,可以試試以下的方式:

使用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[]數組來接收數據 ,Object[]中的每一個元素值就是對應列的值

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

先查出Person ,用java代碼轉換成PersonResult

標簽: java
相關文章:
国产综合久久一区二区三区