Ask Your Question
2

For update mapper, MapStruct produces a mapper method body that is void.

asked 2021-06-03 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-12-16 21:00:00 +0000

qstack gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-03 11:00:00 +0000

Seen: 7 times

Last updated: Dec 16 '21