model mapper not being able to map a nested entity to a dto

73 Views Asked by At

So I have a Review entity and a Campaign entity with each of its own DTOs. I put the Campaign entity as a whole field to the Review entity and put all the Campaign entity fields in the ReviewDTO field as well. When using modelMapper to map the entity to DTO, the review entity will be mapped but it doesn't seem to be getting mapped with campaign fields.

i tried all sorts of ways and using TypeMap configuration on modelMapping worked but my teacher tells me it's not an efficient way to do so and that there will be other easier ways.

I'll provide as much as I can. And with the my current situation, I did see a lot of typemap solutions and it works but I'm just curious if there really is a shorter and much more efficient way to do so.

Review Entity :

@Entity(name="reviewPkg_entityReview") 
@Table(name="tbl_campaign_review") 
public class Review {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "campaign_rev_code")
private Long campaignRevCode;

@Column(name = "review_title")
private String reviewTitle;

@Column(name = "description")
private String description;

@ManyToOne
@JoinColumn(name = "campaign_code")
private Campaign campaign;

public Review(Long campaignRevCode, String reviewTitle, String description, Campaign campaign) {
}

public Review builder() {
    return new Review(campaignRevCode, reviewTitle, description, campaign);
}

public Review campaignRevCode(Long val) {
    this.campaignRevCode = val;
    return this;
}

public Review review_title(String val) {
    this.reviewTitle = val;
    return this;
}

public Review description(String val) {
    this.description = val;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "campaign_rev_code")
private Long campaignRevCode;

@Column(name = "review_title")
private String reviewTitle;

@Column(name = "description")
private String description;

@ManyToOne
@JoinColumn(name = "campaign_code")
private Campaign campaign;

public Review(Long campaignRevCode, String reviewTitle, String description, Campaign campaign) {
}

public Review builder() {
    return new Review(campaignRevCode, reviewTitle, description, campaign);
}

public Review campaignRevCode(Long val) {
    this.campaignRevCode = val;
    return this;
}

public Review review_title(String val) {
    this.reviewTitle = val;
    return this;
}

public Review description(String val) {
    this.description = val;
    return this;
}

public Review campaign(Campaign val) {
this.campaign =val;
return this;
}
protected Review() {
}

public Long getCampaignRevCode() {
    return campaignRevCode;
}

public String getReviewTitle() {
    return reviewTitle;
}

public String getDescription() {
    return description;
}
public Campaign getCampaign() {
    return campaign;
}

@Override
public String toString() {
    return "CampaignRepository{" +
            "campaignRevCode=" + campaignRevCode +
            ", reviewTitle='" + reviewTitle + '\'' +
            ", description='" + description + '\'' +
            ", campaign=" + campaign +
            '}';
}
}

Campaign Entity:

@Entity(name="reviewPkg_entityCampaign") 
@Table(name="tbl_campaign_description") 
public class Campaign {

@Id
@Column(name = "campaign_code")
private Long campaignCode;

@Column(name = "campaign_title")
private String campaignTitle;

@Column(name = "campaign_content")
private String campaignContent;

@Column(name = "start_date")
private java.sql.Date startDate;

@Column(name = "end_date")
private java.sql.Date endDate;

@Column(name = "campaign_category")
private String campaignCategory;

@Column(name = "current_budget")
private Integer currentBudget;

@Column(name = "goal_budget")
private Integer goalBudget;

@Column(name = "org_name")
private String orgName;

@Column(name = "org_description")
private String orgDescription;

@Column(name = "org_tel")
private String orgTel;

protected Campaign() {
}

public Campaign(Long campaignCode, String campaignTitle, String campaignContent, Date startDate, Date endDate, String campaignCategory, Integer currentBudget, Integer goalBudget, String orgName, String orgDescription, String orgTel) {
    this.campaignCode = campaignCode;
    this.campaignTitle = campaignTitle;
    this.campaignContent = campaignContent;
    this.startDate = startDate;
    this.endDate = endDate;
    this.campaignCategory = campaignCategory;
    this.currentBudget = currentBudget;
    this.goalBudget = goalBudget;
    this.orgName = orgName;
    this.orgDescription = orgDescription;
    this.orgTel = orgTel;
}

public Long getCampaignCode() {
    return campaignCode;
}

public void setCampaignCode(Long campaignCode) {
    this.campaignCode = campaignCode;
}


public String getCampaignTitle() {
    return campaignTitle;
}

public void setCampaignTitle(String campaignTitle) {
    this.campaignTitle = campaignTitle;
}

public String getCampaignContent() {
    return campaignContent;
}

public void setCampaignContent(String campaignContent) {
    this.campaignContent = campaignContent;
}

public Date getStartDate() {
    return startDate;
}

public void setStartDate(Date startDate) {
    this.startDate = startDate;
}

public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

public String getCampaignCategory() {
    return campaignCategory;
}

public void setCampaignCategory(String campaignCategory) {
    this.campaignCategory = campaignCategory;
}

public Integer getCurrentBudget() {
    return currentBudget;
}

public void setCurrentBudget(Integer currentBudget) {
    this.currentBudget = currentBudget;
}

public Integer getGoalBudget() {
    return goalBudget;
}

public void setGoalBudget(Integer goalBudget) {
    this.goalBudget = goalBudget;
}

public String getOrgName() {
    return orgName;
}

public void setOrgName(String orgName) {
    this.orgName = orgName;
}

public String getOrgDescription() {
    return orgDescription;
}

public void setOrgDescription(String orgDescription) {
    this.orgDescription = orgDescription;
}

public String getOrgTel() {
    return orgTel;
}

public void setOrgTel(String orgTel) {
    this.orgTel = orgTel;
}

@Override
public String toString() {
    return "Campaign{" +
            "campaignCode=" + campaignCode +
            ", campaignTitle='" + campaignTitle + '\'' +
            ", campaignContent='" + campaignContent + '\'' +
            ", startDate=" + startDate +
            ", endDate=" + endDate +
            ", campaignCategory='" + campaignCategory + '\'' +
            ", currentBudget=" + currentBudget +
            ", goalBudget=" + goalBudget +
            ", orgName='" + orgName + '\'' +
            ", orgDescription='" + orgDescription + '\'' +
            ", orgTel='" + orgTel + '\'' +
            '}';
}

}

my controller:

@GetMapping("/")
public ResponseEntity<List<ReviewDTO>> getReviewList() {

    System.out.println("(ReviewController) 들어옴");

    List<ReviewDTO> details = reviewService.findAllReviews();

    System.out.println("(review controller): 가져온 총 결과 : " +details);
    return  ResponseEntity.ok(details);

}

my service:

public List<ReviewDTO> findAllReviews() {
    List<Review> reviewList = reviewRepository.findAll();

    System.out.println("findAllReviews: " + reviewList);

    return reviewList.stream()
            .map(review -> modelMapper.map(review, ReviewDTO.class))
            .collect(Collectors.toList());
}

and my reviewDTO:

package metaint.replanet.rest.reviews.dto;

import java.sql.Date;

public class ReviewDTO extends CampaignDTO{

private Long campaignRevCode;
private String reviewTitle;
private String description;

private Long campaignCode;
private String campaignTitle;
private String campaignContent;
private Date startDate;
private Date endDate;
private String campaignCategory;
private Integer currentBudget;
private Integer goalBudget;
private String orgName;
private String orgDescription;
private String orgTel;

public ReviewDTO() {
}

public ReviewDTO(Long campaignRevCode, String reviewTitle, String description, Long campaignCode, String campaignTitle, String campaignContent, Date startDate, Date endDate, String campaignCategory, Integer currentBudget, Integer goalBudget, String orgName, String orgDescription, String orgTel) {
    this.campaignRevCode = campaignRevCode;
    this.reviewTitle = reviewTitle;
    this.description = description;
    this.campaignCode = campaignCode;
    this.campaignTitle = campaignTitle;
    this.campaignContent = campaignContent;
    this.startDate = startDate;
    this.endDate = endDate;
    this.campaignCategory = campaignCategory;
    this.currentBudget = currentBudget;
    this.goalBudget = goalBudget;
    this.orgName = orgName;
    this.orgDescription = orgDescription;
    this.orgTel = orgTel;
}

public ReviewDTO(Long campaignCode, String campaignTitle, String campaignContent, java.util.Date startDate, java.util.Date endDate, String campaignCategory, Integer currentBudget, Integer goalBudget, String orgName, String orgDescription, String orgTel, Long campaignRevCode, String reviewTitle, String description, Long campaignCode1, String campaignTitle1, String campaignContent1, Date startDate1, Date endDate1, String campaignCategory1, Integer currentBudget1, Integer goalBudget1, String orgName1, String orgDescription1, String orgTel1) {
    super(campaignCode, campaignTitle, campaignContent, startDate, endDate, campaignCategory, currentBudget, goalBudget, orgName, orgDescription, orgTel);
    this.campaignRevCode = campaignRevCode;
    this.reviewTitle = reviewTitle;
    this.description = description;
    this.campaignCode = campaignCode1;
    this.campaignTitle = campaignTitle1;
    this.campaignContent = campaignContent1;
    this.startDate = startDate1;
    this.endDate = endDate1;
    this.campaignCategory = campaignCategory1;
    this.currentBudget = currentBudget1;
    this.goalBudget = goalBudget1;
    this.orgName = orgName1;
    this.orgDescription = orgDescription1;
    this.orgTel = orgTel1;
}

public Long getCampaignRevCode() {
    return campaignRevCode;
}

public void setCampaignRevCode(Long campaignRevCode) {
    this.campaignRevCode = campaignRevCode;
}

public String getReviewTitle() {
    return reviewTitle;
}

public void setReviewTitle(String reviewTitle) {
    this.reviewTitle = reviewTitle;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@Override
public Long getCampaignCode() {
    return campaignCode;
}

@Override
public void setCampaignCode(Long campaignCode) {
    this.campaignCode = campaignCode;
}

@Override
public String getCampaignTitle() {
    return campaignTitle;
}

@Override
public void setCampaignTitle(String campaignTitle) {
    this.campaignTitle = campaignTitle;
}

@Override
public String getCampaignContent() {
    return campaignContent;
}

@Override
public void setCampaignContent(String campaignContent) {
    this.campaignContent = campaignContent;
}

@Override
public Date getStartDate() {
    return startDate;
}

public void setStartDate(Date startDate) {
    this.startDate = startDate;
}

@Override
public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

@Override
public String getCampaignCategory() {
    return campaignCategory;
}

@Override
public void setCampaignCategory(String campaignCategory) {
    this.campaignCategory = campaignCategory;
}

@Override
public Integer getCurrentBudget() {
    return currentBudget;
}

@Override
public void setCurrentBudget(Integer currentBudget) {
    this.currentBudget = currentBudget;
}

@Override
public Integer getGoalBudget() {
    return goalBudget;
}

@Override
public void setGoalBudget(Integer goalBudget) {
    this.goalBudget = goalBudget;
}

@Override
public String getOrgName() {
    return orgName;
}

@Override
public void setOrgName(String orgName) {
    this.orgName = orgName;
}

@Override
public String getOrgDescription() {
    return orgDescription;
}

@Override
public void setOrgDescription(String orgDescription) {
    this.orgDescription = orgDescription;
}

@Override
public String getOrgTel() {
    return orgTel;
}

@Override
public void setOrgTel(String orgTel) {
    this.orgTel = orgTel;
}

@Override
public String toString() {
    return "ReviewDTO{" +
            "campaignRevCode=" + campaignRevCode +
            ", reviewTitle='" + reviewTitle + '\'' +
            ", description='" + description + '\'' +
            ", campaignCode=" + campaignCode +
            ", campaignTitle='" + campaignTitle + '\'' +
            ", campaignContent='" + campaignContent + '\'' +
            ", startDate=" + startDate +
            ", endDate=" + endDate +
            ", campaignCategory='" + campaignCategory + '\'' +
            ", currentBudget=" + currentBudget +
            ", goalBudget=" + goalBudget +
            ", orgName='" + orgName + '\'' +
            ", orgDescription='" + orgDescription + '\'' +
            ", orgTel='" + orgTel + '\'' +
            '}';
}
}
0

There are 0 best solutions below