java - Jpa返回对象必须是与Entity类么?
迷茫
迷茫 2017-04-18 10:56:32
0
1
826
  @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嘗試轉換成你自訂的對象,但是失敗了,可以試試以下的方式:

  1. 使用select new +物件全類別名稱 的語法,
    此處的Perso 為EntityManager 管理的實體,PersonResult為自訂的實體

    @Query(select new com.xx.yy.PersonResult(p.id,p.name,p.age) from Person p)
     List<PersonResult> findPersonResult();
  2. 使用Object[]陣列來接收資料 ,Object[]中的每一個元素值就是對應列的值

    @Query(select p.id,p.name,p.age from Person p)
     List<Object[]> findPersonResult();
  3. 先查出Person ,用java程式碼轉換成PersonResult

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!