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, 再发表一次看看那}’; 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
相关文章:
1. 为什么span的color非要内联样式才起作用?2. 小白问题getDay()3. html5 - H5做的手机分享页微信更新后,分享出去不再默认显示第一个图 作为缩略图4. golang - 用IDE看docker源码时的小问题5. docker start -a dockername 老是卡住,什么情况?6. 手机开发 - Android蓝牙模块连接后怎么接收数据?求助7. css - div设置float:left后高度设置自动会无效 ?8. 请问一下各位老鸟 我一直在学习独孤九贱 现在是在tp5 今天发现 这个系列视频没有实战9. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?10. 老师,请问我打开browsersync出现这个问题怎么解决啊?

网公网安备