查找以下实体类:
@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
}
查找以下DTO课程
public class RoomDTO{
private String hotelName;
private String code;
private String type;
//getters and setters
}
我编写了以下映射器代码,用于将数据从RoomEntity复制到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);
}
}
属性 - 代码和类型值将从RoomEntity复制到RoomDTO
但是不会复制嵌套属性值( hotel.name - &gt; hotelName )。
请帮助解决此问题。