【问题标题】:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast or BeanUtils.copyProperties not workingjava.lang.ClassCastException:[Ljava.lang.Object;无法强制转换或 BeanUtils.copyProperties 不起作用
【发布时间】:2021-06-04 00:42:45
【问题描述】:

我是 JPA 新手,使用 @Query 参数时 springboot 无法获得 api 响应(我尝试实现内部连接)

存储库类:

 @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("select A.id, A.position ,A.title,A.shortdescription,A.thumbnailimage, A.linkactions, A.last_updated_date, A.last_updated_by, B.countryName " +
            "from ContentManage as A inner join Country as B on A.countryid=B.countryId")
    List<ContentManage> fetchDataInnerJoin();

服务类:

public ContentManageListResponse queryAllActions() {
        List<ContentManage> contentManageList = contentManageRepository.fetchDataInnerJoin();
        List<ContentManageVO> contentManageVOList = new ArrayList<>();
        for (ContentManage contentManage : contentManageList) {
            ContentManageVO contentManageVO = new ContentManageVO();
            BeanUtils.copyProperties(contentManage,contentManageVO);
            contentManageVOList.add(contentManageVO);
        }
        return ContentManageListResponse.builder().contents(contentManageVOList).build();
    }

我得到了“[Ljava.lang.Object; cannot be cast”异常,之后我改为如下:

服务等级

public ContentManageListResponse queryAllActions() {
        List<ContentManage> contentManageList = contentManageRepository.fetchDataInnerJoin();
        List<ContentManageVO> contentManageVOList = new ArrayList<>();
        for (Object contentManage : contentManageList) {
            ContentManageVO contentManageVO = new ContentManageVO();
            BeanUtils.copyProperties(contentManage,contentManageVO);
            contentManageVOList.add(contentManageVO);
        }
        return ContentManageListResponse.builder().contents(contentManageVOList).build();
    }

foreach 添加了对象,但是对于上面的代码,我得到了空值 BeanUtils.copyProperties 不起作用

请任何人建议如何解决此问题。

【问题讨论】:

    标签: java spring-boot hibernate rest jpa


    【解决方案1】:

    你的陈述

    @Query("select A.id, A.position ,A.title,A.shortdescription,A.thumbnailimage, A.linkactions, A.last_updated_date, A.last_updated_by, B.countryName " +
                "from ContentManage as A inner join Country as B on A.countryid=B.countryId")
    

    在选择中包含来自ContentManageCountry 的部分。 但你的结果只是一个List&lt;ContentManage&gt;

    要解决这个问题,您可以创建一个新的 Dto 类,其中包含您需要的 A 和 B 的所有字段。这个 Dto 类必须有一个全参数的构造函数。然后代替

     "select A.id, A.position ,A.title,A.shortdescription,A.thumbnailimage, A.linkactions, A.last_updated_date, A.last_updated_by, B.countryName " +
                "from ContentManage as A inner join Country as B on A.countryid=B.countryId"
    

    你可以写:

    "select new com.you.package.YourDtoClass (A.id, A.position ,A.title,A.shortdescription,A.thumbnailimage, A.linkactions, A.last_updated_date, A.last_updated_by, B.countryName) " +
                "from ContentManage as A inner join Country as B on A.countryid=B.countryId"
    

    【讨论】:

    • 感谢它在我创建 Dto 类时的工作。
    【解决方案2】:
    Controller.java
    ===============
    
    @Autowired
        private Service Service;
        
    @ApiHeader(
                apiOperation = "get all Content Manage",
                apiOperationNotes = "get all Content Manage"
        )
        @GetMapping(value = UriConstants.CONTENT_MANAGE_QUERY,produces = {MediaType.APPLICATION_JSON_VALUE})
    
        public ResponseEntity<CMListResponse> queryAllCM(
                @RequestHeader HttpHeaders apiRequest){
            CMListResponse response = CMService.queryAllCM();
            return ResponseEntity.ok(response);
        }
        
    
    CMListResponse.java
    ==============================
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public class CMListResponse {
        List<CMVO> contents;
    }
    
    CMVO.java
    =====================
    @Setter
    @Getter
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public class CMVO {
    
        private static final long serialVersionUID = 92012330342289044L;
    
        @ApiModelProperty(value = "id", example = "123")
        @JsonProperty(value = "id")
        private int id;
    
        @ApiModelProperty(value = "position", example = "123")
        @JsonProperty(value = "position")
        private int position;
    
        @ApiModelProperty(value = "title", example = "desc")
        @JsonProperty(value = "title")
        private String title;
    
        @ApiModelProperty(value = "shortDescription", example = "shortDescription")
        @JsonProperty(value = "shortDescription")
        private String shortdescription;
    
        @ApiModelProperty(value = "thumbnailimage", example = "thumbnailimage")
        @JsonProperty(value = "thumbnailimage")
        private String thumbnailimage;
    
        @ApiModelProperty(value = "linkactions", example = "linkactions")
        @JsonProperty(value = "linkactions")
        private String linkactions;
    
        /*@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")*/
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX")
        @ApiModelProperty(value = "last_updated_date", example = "2021-03-17T02:59:24.120Z")
        @JsonProperty(value = "last_updated_date")
        private ZonedDateTime last_updated_date;
    
        @ApiModelProperty(value = "last_updated_by", example = "A9002255")
        @JsonProperty(value = "last_updated_by")
        private String last_updated_by;
    
        @ApiModelProperty(value = "countryid", example = "3023D08B-D861-4514-8E13-FFCF97A3D1DD")
        @JsonProperty(value = "countryid")
        private String countryid;
    
        @ApiModelProperty(value = "countryName", example = "Singapore")
        @JsonProperty(value = "countryName")
        private String countryName;
    
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX")
        @ApiModelProperty(value = "date_published", example = "2021-03-17T02:59:24.120Z")
        @JsonProperty(value = "date_published")
        private ZonedDateTime date_published;
    
        @Override
        public String toString() {
            return "CMVO{" +
                    "id=" + id +
                    ", position=" + position +
                    ", title='" + title + '\'' +
                    ", shortdescription='" + shortdescription + '\'' +
                    ", thumbnailimage='" + thumbnailimage + '\'' +
                    ", linkactions='" + linkactions + '\'' +
                    ", last_updated_date=" + last_updated_date +
                    ", last_updated_by='" + last_updated_by + '\'' +
                    ", countryid='" + countryid + '\'' +
                    ", countryName='" + countryName + '\'' +
                    ", date_published=" + date_published +
                    '}';
        }
    }
    
    service.java
    =============
     private Repository repository;
    
        @Autowired
        public CMService(Repository Repository) {
            this.Repository = Repository;
        }
     public CMListResponse queryAllCM() {
            List<CCDTO> CMList =
                    Repository.fetchAllCMData();
            List<CMVO> CMVOList = new ArrayList<>();
            for (CCDTO CM : CMList) {
                CMVO CMVO = new CMVO();
                BeanUtils.copyProperties(CM,CMVO);
                CMVOList.add(CMVO);
            }
            return CMListResponse.builder().contents(CMVOList).build();
        }
        
        
    CCDTO.java
    ==========
    @Setter
    @Getter
    @Builder
    @AllArgsConstructor
    public class CCDTO {
    
        private  int id;
        private  int position;
        private  String title;
        private  String shortdescription;
        private  String thumbnailimage;
        private  String linkactions;
        private  ZonedDateTime last_updated_date;
        private  String last_updated_by;
        private  String countryid;
        private  String countryName;
        private  ZonedDateTime date_published;
    
    
    
        @Override
        public String toString() {
            return "CCDTO{" +
                    "id=" + id +
                    ", position=" + position +
                    ", title='" + title + '\'' +
                    ", shortdescription='" + shortdescription + '\'' +
                    ", thumbnailimage='" + thumbnailimage + '\'' +
                    ", linkactions='" + linkactions + '\'' +
                    ", last_updated_date=" + last_updated_date +
                    ", last_updated_by='" + last_updated_by + '\'' +
                    ", countryid='" + countryid + '\'' +
                    ", countryName='" + countryName + '\'' +
                    ", date_published=" + date_published +
                    '}';
        }
    }
    
    repository.java
    ===============
    @Transactional(rollbackFor = Exception.class)
        @Query("select new com.model.entity.CCDTO (A.id, A.position ,A.title," +
                "A.shortdescription,A.thumbnailimage, A.linkactions, A.last_updated_date, A.last_updated_by, " +
                "A.country.countryId, A.country.countryName, A.date_published) " +
                "from CM as A inner join A.country as B order by A.id asc")
        List<CCDTO> fetchAllCMData();
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-08
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 2015-10-18
      • 2014-05-09
      相关资源
      最近更新 更多