0
votes

I am having a problem where i have one dto class like

Class Vehicle{
  private int id;
  private String name;
  private UUID vehicleId;
}

and one Entity class like

Class VehicleEntity{
 private int id;
 private String name;
 private Car car;
}

and I am having a mapper where i am mapping both the class in service layer.

Now the question is , how mapper will get the values from dto and will set it in entity while in dto all variables are instance and in entity there is one object reference 'Car'.?

Do I need to need to explicitly map this relation of 'vehicleId' and 'Car'. Or mapper will automatically handle this?

1

1 Answers

0
votes

Assuming that vehicleId is actually the id of the Car. You can use nested source parameters to define that mapping. A mapper can look like:

@Mapper
public interface VehicleMapper {

    @Mapping(target = "vehicleId", source = "car.id")
    Vehicle map(VehicleEntity vehicle);

}

MapStruct will generate a method to get the id of the car from the vehicle