当前位置: 首页 > 知识库问答 >
问题:

java.lang.ClassCastException:[Ljava.lang.Object;无法施放或BeanUtils.copyProperties不工作

慕容晔
2023-03-14

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

Repositoty类:

 @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;无法强制转换”异常,之后我将其更改为如下:

服务等级

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添加了对象,但是对于上面的代码,我得到了空值<code>BeanUtils。copyProperties不起作用

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

共有2个答案

南门峰
2023-03-14
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();

江高飞
2023-03-14

你的陈述

@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")

包含select中< code>ContentManage和< code>Country的部分。但是您的结果只是一个< code >列表

为了解决这个问题,您可以创建一个新的Dto类,该类包含所需的a和B中的所有字段。此Dto类必须具有all args构造函数。然后代替

 "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"
 类似资料:
  • 问题内容: 为什么在我的程序中触发java.lang.ClassCastException? java.lang.ClassCastException:[Ljava.lang.Object; 无法转换为com.App.Equip] 该查询返回已回答(在CheckLists calsse中找到)但尚未由Equip对象回答的清单的列表 -这是代码: -我的java类: 我想将我的SQl查询的结果格式化

  • 问题内容: 我需要将HashMap转换为String数组,以下是我的Java代码 当我运行代码时,得到以下内容。 问题答案: 返回,而不管泛型。您可以改用重载的变体: 另外,由于的方法不能保证顺序,并且您使用数组进行的所有操作都会打印出值,因此可以直接迭代: 编辑:只是为了完成图片,在Java 8中,该方法可用于使代码更加优雅:

  • 问题内容: 我想在休眠状态查询两个表。用户实体中的featch 3表(User-Role-Profile)。用hql查询: 并运行查询: userentity类:此类是geteer和seter: userEntity.hbm.xml的休眠映射 和类hibernateutil创建会话: 问题答案: 因为使用的是多选投影,所以实际上是在获取对象数组,因此需要将查询结果处理逻辑更改为:

  • 问题内容: 我正在运行以下查询。它显示一条错误消息。如何解决这个错误? 错误是: 问题答案: 您可以这样写: 已经是一个字符串列表,因为您只选择了一列。 此外,正如Thilo的答案所暗示的那样,您可以将结果直接转换为a 而不是使用。

  • 问题内容: 码: CrbtSubMasterDemo 与数据库一起映射。当我尝试运行它时,它显示以下异常: 问题是query.list()返回pojo类的对象列表。那为什么是这个异常。我是Hibernate的新手,很抱歉这是一个愚蠢的问题。 问题答案: 先生,许多用户都面临这种需求。Hibernate具有ResultTransformer来在Object中转换hql / sql。 它将您查询转换为

  • 问题内容: 我想在hibernate状态查询两个表。用户实体中的featch 3表(User-Role-Profile)。用hql查询: 并运行查询: userentity类:此类是geteer和seter: userEntity.hbm.xml的hibernate映射 和类hibernateutil创建会话: 问题答案: 由于使用的是多选投影,因此实际上是在获取对象数组,因此需要将查询结果处理逻