6

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.

Stefan Radonjic
  • 1,449
  • 4
  • 19
  • 38
  • 1
    I haven't used `ModelMapper` before, but I think you need to check the matching strategy: [link](http://modelmapper.org/user-manual/configuration/#matching-strategies). – Luay Abdulraheem May 15 '18 at 01:58

1 Answers1

12

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.

Mo Hamid
  • 471
  • 5
  • 9
  • It is using a lombok `@Getter` `@Setter`, basically generates the setters/getters. Is there a known issue with lombok and model mapper? – lemoncodes Apr 07 '20 at 13:07
  • @lemoncodes yes, when you forget these to apply on your class – ImtiazeA Apr 14 '20 at 16:42
  • I was facing the same problem and was wondering why I get the nulls - seems because I removed the unused getters/setters, thinking that @Builder is enough. Added the getter/setter Lombok annotations, this solved my problem. Thanks! – Akabelle Oct 09 '20 at 13:54