Find below entity classes :
@Entity
@Table(name="rooms")
public class RoomEntity {
@Column(name="mr_code", length=50, nullable=false)
private String code;
@Column(name="mr_roomtype", nullable=false, length=50)
private String type;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "mr__hotelId", nullable = false)
private HotelEntity hotel;
//getters and setters
}
@Entity
@Table(name="hotels")
public class HotelEntity{
@Column(name="mh_name", nullable=false)
private String name;
@Column(name="mh_description")
private String description;
@OneToMany(mappedBy = "hotel", fetch=FetchType.EAGER)
private Set<RoomEntity> rooms = new HashSet<>(0);
//getters and setters
}
Find below DTO class
public class RoomDTO{
private String hotelName;
private String code;
private String type;
//getters and setters
}
I written the below mapper code to copy data from RoomEntity to RoomDTO
public class BeanMapper {
private static MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
public static RoomDTO toRoomDTO(RoomEntity roomEntity) {
mapperFactory.classMap(RoomEntity.class, RoomDTO.class).field("hotel.name","hotelName").byDefault().register();
MapperFacade mapper = mapperFactory.getMapperFacade();
return mapper.map(roomEntity,RoomDTO.class);
}
}
The properties - code and type values are getting copied from RoomEntity to RoomDTO.
But the nested properties values(hotel.name -> hotelName) are not getting copied.
Please help to resolve this issue.