简体   繁体   English

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

[英]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:这是我的 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";
    }

}

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 .当我打开 Postman 并尝试从MySQL数据库中获取所有Accommodation对象时,我实际上想要获取 DTO 对象,为此我使用了ModelMapper But for some reason every time I try to map Accommodation to AccommodationView , I get Null in return.但是出于某种原因,每次我尝试将Accommodation映射到AccommodationView ,我都会得到 Null 作为回报。 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.输出的第一部分来自Accommodation对象,输出的第二部分来自AccommodationView对象。 If anyone has any idea whats going on I would really appreciate the help.如果有人知道发生了什么,我将非常感谢您的帮助。

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.否则 Modelmapper 无法访问您的类的私有字段来设置它们的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ModelMapper 将外键从 dto 映射到实体对象? - How to map foreign key from dto to the entity object using ModelMapper? Java ModelMapper:将 DTO 映射到 EmbeddedId 实体类 - Java ModelMapper: map DTO to EmbeddedId entity class modelmapper 制作的 Spring DTO 返回所有内容 null - Spring DTO made by modelmapper returns everything null ModelMapper 在从实体映射到 DTO 时在 DTO 字段中返回 Null - ModelMapper return Null in DTO fields while mapping from Entity to DTO ModelMapper DTO 到实体 - ModelMapper DTO to Entity ModelMapper &amp; JPA:如何将带有 id 的 map DTO 转换为带有 oneToMany 字段的实体 - ModelMapper & JPA: How to map DTO with id to Entity with oneToMany field 是否可以对 ModelMapper(在 spring 引导中)使用一个 propertyMap 到 map 在两个方向(DTO 到实体和实体到 DTO)? - Is it possible to use one propertyMap for ModelMapper (in spring boot) to map in both directions (DTO to Entity and Entity to DTO)? 如何使用 ModelMapper 将两个相关实体映射到一个 DTO 对象 - How Map two related Entities to one DTO Object using ModelMapper 使用 ModelMapper 将复杂实体转换为 DTO - Convert Complex Entity to DTO With ModelMapper 来自模拟 ModelMapper 的 map() 方法返回 null 值 - map() method from mocked ModelMapper returns null value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM