2

am new to JPA and springboot unable to get the api response when am using @Query param(I try achieve the inner join)

Repositoty class:

 @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();

Service class:

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();
    }

am getting the " [Ljava.lang.Object; cannot be cast " exception after that I have changed to as below:

service class

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 added the Object but for the above code am getting null values BeanUtils.copyProperties is not working

please any one suggest how to fix this.

2 Answers 2

1

Your statement

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

contains parts from ContentManage and Country in select. But your result is only a List<ContentManage>

To solve this you can create a new Dto class containing all the fields from A and from B you need. This Dto class must have an all-args constructor. Then instead of

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

you can write:

"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"
Sign up to request clarification or add additional context in comments.

1 Comment

thank you its working when I have created the Dto class.
0
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.