gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:28:03 25 4
gpt4 key购买 nike

这是我尝试映射的对象的类:

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

}

当我打开 Postman 并尝试从 MySQL 数据库获取所有 Accommodation 对象时,我实际上想要获取 DTO 对象,为此我使用 ModelMapper。但由于某种原因,每次我尝试将 Accommodation 映射到 AccommodationView 时,我都会得到 Null 作为返回。这是我尝试执行映射的类:

@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 对象,输出的第二部分来自 AccommodationView 对象。如果有人知道发生了什么事,我将非常感谢您的帮助。

最佳答案

您必须为您的情况(住宿实体)的目标类生成公共(public)设置函数。否则,模型映射器无法访问类的私有(private)字段来设置它们的值。

关于java - 尝试将实体映射到 DTO 对象时 ModelMapper 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50319545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com