Reputation: 1598
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.
Upvotes: 7
Views: 16517
Reputation: 1149
I suggest two solutions to your problem.
Solution 1
In your dto object AccommodationView try to add @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
. Or add for every dto field annotation @JsonPrperty
. that two ways could help to map dto object and json postman response.
Solution 2
Seems that the problem you have is about mapstruct or/and lombok. I would recommand you to check if generated mapper impl mapstruct file ModelMapperImpl looks good (you should have for every entity field an instruction for mapping with dto associated field or/and vice versa).
If this is not the case, check if lombok, mapstruct and lombok-mapstruct-binding deps are well configured in you pom.xml or build.gradle file ( here an example for this implementation with maven ). Also, For your IDE check if lombok is well configured (by installing lombok plugins and enabling annotation processors)
Upvotes: 0
Reputation: 501
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.
Upvotes: 12