spring data JPA 类型转换异常

工作中使用spring data jpa 遇到一个百思不得姐的问题,劳烦各位大佬帮忙指点迷津。,小老弟感激不尽!!

            DAO :
                            package com.xhsceis.credit.repository.firstScreenRepository;

                                import com.xhsceis.credit.entity.FsManageStatus;
                                import org.springframework.data.jpa.repository.JpaRepository;
                                import org.springframework.data.jpa.repository.Query;

                                import java.util.List;

                                public interface FsManageStatusRepository extends JpaRepository<FsManageStatus,String> {

                                        @Query(value = "SELECT FS.changPing,FS.chaoYang FROM FsManageStatus FS")
                                        List<FsManageStatus> getFsManageStatus();
                                }


        SERVICE:
                             List<FsManageStatus> fsManageStatusList = fsManageStatusRepository.getFsManageStatus();

                            for **(FsManageStatus fsManageStatus : fsManageStatusList)** {
                                            Map<String, Object> fsManage = Maps.newHashMap();
                                            Map<String, Object> data = Maps.newHashMap();
                                            data.put("changPing", fsManageStatus.getChangPing());
                                            data.put("chaoYang", fsManageStatus.getChaoYang());

                                }

        ENTITY:

                @Entity
                @Table(name = "FS_MANAGE_STATUS")
                public class FsManageStatus {

                    @Id
                    @Column(name = "STATUS")
                    private String status;
                    @Column(name = "DONG_CHENG")
                    private String dongCheng;
                    @Column(name = "FENG_TAI")
                    private String fengTai;
                    @Column(name = "DA_XING")
                    private String daXing;
                    @Column(name = "MI_YUN")
                    private String miYun;
                    @Column(name = "PING_GU")
                    private String pingGu;
                    @Column(name = "YAN_QING")
                    private String yanQing;
                    @Column(name = "HUAI_ROU")
                    private String huaiRou;
                    @Column(name = "FANG_SHAN")
                    private String fangShan;
                    @Column(name = "CHANG_PING")
                    private String changPing;
                    @Column(name = "CHAO_YANG")
                    private String chaoYang;
                    @Column(name = "HAI_DIAN")
                    private String haiDian;
                    @Column(name = "SHI_JING_SHAN")
                    private String shiJingShan;
                    @Column(name = "XI_CHENG")
                    private String xiCheng;
                    @Column(name = "TONG_ZHOU")
                    private String tongZhou;
                    @Column(name = "MEN_TOU_GOU")
                    private String menTouGou;
                    @Column(name = "SHUN_YI")
                    private String shunYi;
                    @Column(name = "COUNT")
                    private String count;

问题描述:service 查出来的数据对象集合,我遍历的时候,报错说类型转换异常
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xhsceis.credit.entity.FsManageStatus
我猜应该是jpa查到的数据没有给指定成集合对象来接收,而是自己默认为object存储的。这是我的猜想,最后,还是得麻烦大佬给解答一哈儿。

2个回答

你可以尝试使用

 @Query(value = "SELECT FS FROM FsManageStatus FS")
List<FsManageStatus> getFsManageStatus();

或者尝试

 @Query(value = "SELECT FS.changPing,FS.chaoYang FROM FsManageStatus FS")
    List<Object[]> getFsManageStatus();

添加有参构造函数试试呢!

   public FsManageStatus(String changPing, String chaoYang )
    {
        super();
        this.changPing = changPing;
        this.chaoYang  = chaoYang ;
    }

回答完毕!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问