Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

This means that when using MapStruct for updating existing objects, the mapper method only modifies the target object and does not return any value. The generated code directly updates the target object with the values from the source object, without creating a new object or returning anything.

For example, consider the following mapper interface that updates a Customer object with data from a CustomerDto object:

@Mapper
public interface CustomerMapper {
    void updateCustomerFromDto(CustomerDto dto, @MappingTarget Customer customer);
}

In this case, the `````` method takes a CustomerDto object as input, and updates an existing Customer object with the data from the DTO. The method is marked with @MappingTarget annotation to indicate that the second parameter is the object that needs to be updated with the values from the DTO.

The generated implementation of this method will directly update the Customer object with the values from the DTO, without creating a new object or returning any value. Here's an example of how you can use this mapper method to update an existing Customer object:

CustomerMapper mapper = Mappers.getMapper(CustomerMapper.class);

// existing customer object
Customer customer = ... 

// create a new DTO with updated data
CustomerDto dto = new CustomerDto();
dto.setName("John Doe");
dto.setEmail("john.doe@example.com");

// update the existing object with the new data
mapper.updateCustomerFromDto(dto, customer);

In this example, the method updates the object with the name and email from the `````` object. The updated object is returned directly, without creating a new object or returning any value.