尝试将实体映射到DTO对象时,ModelMapper返回NULL

时间:2018-05-13 18:49:56

标签: java spring-boot dto modelmapper

这是我想要映射的对象的类:

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

}

这是我的DTO对象:

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

}

当我打开我的邮递员并尝试从 MySQL 数据库中获取所有Accommodation个对象时,我实际上想要获取DTO对象,为了做到这一点,我使用{{ 1}}。但由于某种原因,每当我尝试将ModelMapper映射到Accommodation时,我都会得到Null作为回报。这是我尝试执行映射的类:

AccommodationView

这是我点击方法时得到的输出:

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

输出的第一部分来自Name: Test Agent PIB: 2308995710368 ID: null Type: null Description: null Category: null Name: null 对象,输出的第二部分来自Accommodation对象。如果有人有任何想法,我会非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

在您的情况下(住宿实体),您必须为目标类生成公共设置器函数。否则,Modelmapper无法访问类的私有字段来设置其值。