[英]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.