我有2个实体:
实体1:
public class Master {
private int id;
private Set<SubMaster> subMasters= new HashSet<SubMaster>(0);
}
public class SubMaster{
private int subId;
private String subName;
}
Run Code Online (Sandbox Code Playgroud)
实体2:
public class MasterDTO {
private int id;
private Set<SubMaster> subMasters= new HashSet<SubMaster>(0);
}
public class SubMasterDTO{
private int subId;
private String subName;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用MapStruct映射器将POJO的值映射到另一个。
public interface MasterMapper{
MasterDTO toDto(Master entity);
}
Run Code Online (Sandbox Code Playgroud)
我能够成功映射Master到MasterDTO。但是,SubMasterin 的嵌套集合Master未映射到in中的对应集合MasterDTO。
谁能在正确的方向帮助我?
因此,我有一个VehicleDto:
class VehicleDto {
private String someId
private String vType;
private CarDto car;
private BikeDto bike;
}
Run Code Online (Sandbox Code Playgroud)
我需要在请求有效负载中包含CarDto或BikeDto。
在请求后的有效负载中,将存在多个字段,这些字段是VehicleDto的属性,例如,此处为someId。现在,这个someId也是CarDto和BikeDto的一部分,以及是VehicleDto的子代的任何其他Dto。
因此,当我尝试保存到数据库中时,那里存在一些问题。
if (vehicleDto.getVType().equals("CAR")) {
this.saveCar(vehicleDto);
}
private boolean saveCar(TicketSoldCreateDto ticketSoldCreateDto) {
CarDto carDto = ticketSoldCreateDto.getCar();
carDto is mapped to Car model
// Now how do I map the rest of the fields in vehicleDto to Car model??
}
Run Code Online (Sandbox Code Playgroud)
特级车:
@MappedSuperclass
@Data
public abstract class Vehicle extends AbstractBaseEntity {
// fields same as vehicleDto
}
Run Code Online (Sandbox Code Playgroud)
童车:
@Entity
@Data
public class Car extends …Run Code Online (Sandbox Code Playgroud)