Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModelMapper returns NULL when trying to map Entity to DTO object

Here is the class of the object I am trying to map:

package com.agent.module.entities;


import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

@Entity
@Getter @Setter @NoArgsConstructor
@Accessors
public class Accommodation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String location;
    @ManyToOne(optional=false, fetch=FetchType.EAGER)
    private AccommodationType type;

    private String description;

    private String name;
    @OneToMany(orphanRemoval=true, fetch=FetchType.EAGER)
    @Cascade(CascadeType.ALL)
    private Set<Document> images;

    private Integer capacity;
    @ManyToMany(fetch=FetchType.EAGER)
    private Set<AdditionalService> additionalServices;

    @OneToMany(orphanRemoval=true, fetch=FetchType.EAGER)
    @Cascade(CascadeType.ALL)
    private Set<PricePlan> pricePlan;

    @ManyToOne(optional=false, fetch=FetchType.LAZY)
    private Agent agent;

    @OneToMany(orphanRemoval=true, mappedBy="accommodation", fetch=FetchType.EAGER)
    @Cascade(CascadeType.ALL)
    private Set<Restriction> restrictions;

    @ManyToOne(fetch=FetchType.EAGER)
    private Category category;

    @Override
    public String toString() {
        return "Name: "+name+"\n"+"Agent PIB: "+agent.toString()+"\n";
    }

}

And here is my DTO object:

package com.agent.module.dto;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter @Setter @NoArgsConstructor
@XmlRootElement
public class AccommodationView {
    private Long id;
    private String location;
    private String typeName;
    private String description;
    private String name;
    private List<String> imagesPath;
    private Integer capacity;
    private List<String> additionalServicesName;
    private List<PricePlanView> pricePlan;
    private String agentUsername;
    private List<RestrictionView> restrictions;
    private String categoryName;

    @Override
    public String toString() {
        return "ID: "+id+"\n"+"Type: "+typeName+"\n"+"Description: "+description+"\n"+"Category: "+categoryName+"\n"+"Name: "+name+"\n";
    }

}

When I open my Postman and try to get all the Accommodation objects from MySQL database, I actually want to get DTO objects, and in order to do that I am using ModelMapper. But for some reason every time I try to map Accommodation to AccommodationView, I get Null in return. Here is the class where I am trying to perform the mapping:

@RestController
@RequestMapping(value = "/accommodation")
public class AccommodationController {

    @Autowired
    AccommodationRepo accommodationRepo;

    @Autowired 
    ModelMapper mapper;

    @RequestMapping(value="/all",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    ResponseEntity<List<AccommodationView>> getAll(){
        List<Accommodation> accommodations = accommodationRepo.findAll();

        List<AccommodationView> accommodationViewList= new ArrayList<AccommodationView>();

        for(Accommodation accommodation : accommodations) {
            System.out.println(accommodation);
            System.out.println(convertToDto(accommodation));
            accommodationViewList.add(convertToDto(accommodation));
        }

        return new ResponseEntity<List<AccommodationView>>(accommodationViewList, HttpStatus.OK);
    }

    private AccommodationView convertToDto(Accommodation accommodation) {
        return mapper.map(accommodation, AccommodationView.class);
    }

    private Accommodation convertToEntity(AccommodationView accommodationView) {
        return mapper.map(accommodationView, Accommodation.class);
    }
}

Here is the output I get when I hit the method:

Name: Test
Agent PIB: 2308995710368

ID: null
Type: null
Description: null
Category: null
Name: null

First part of the output is from Accommodation object, and second part of the output is from AccommodationView object. If anyone has any idea whats going on I would really appreciate the help.

like image 780
Stefan Radonjic Avatar asked May 13 '18 18:05

Stefan Radonjic


1 Answers

you have to generate public setters functions for the target class, in your case (Accommodation Entity). elsewise the Modelmapper cannot access the private fields of your class to set their values.

like image 69
Mo Hamid Avatar answered Nov 02 '22 14:11

Mo Hamid