5 Essential Features of Mapstruct | Spring Boot Backend #5.2

preview_player
Показать описание
In this video I show 5 essential features of Mapstruct with which you can solve almost all the operations with Mapstruct. It handles 5 basic features of Mapstruct to solve the most part of the problems.

The video contains the following topics:
* the mapping with constants;
* the inclusion of the imports in the implementation class;
* the Java expressions;
* the default methods of the interfaces and its usage in the Java expressions;
* the dependency of the fields sequence;
* the @MappingTarget annotation to update existing objects.

Рекомендации по теме
Комментарии
Автор

Good explanation of important features. Thanks!

omegasilva
Автор

Hi, thank you for all. I would like you takk about annotation @BeforeMapping

moussafall
Автор

at 3:17 how did you get syntax highlighting etc. for the stuff inside java(...) ? It's all a string for me in IntelliJ Ultimate 2021. Anyone else having this?

quickmaths
Автор

Thanks for the video! I found some new ideas for my project. The complicated area when using mapstruct is nested lists/iterable/collections mappings. Could you please cover this may be in new videos?

aziubin
Автор

is there a way to map string[] {'one', 'two'} to List<TargetClass> targets?

venkatr
Автор

Great video. how to sum and count child table? (one to many
)

zenhsuld
Автор

Merci pour les infos. Ça pourrait être intéressant de montrer comment gérer les références circulaires avec mapstruct (comme dans le cadre des relations bidirectionnelles manyToOne oneToMany)

renaudmaxime
Автор

Thank you Sergio for your video. I have a small issue when I have bidirectional relationship. I get recursive call.

Assume the following scenario:

Department {
@Fetch(FetchMode.SELECT)
@OneToMany(mappedBy = "departmentId", cascade = Cascade.ALL, orphanRemoval = true, fetch = FetchType.EAGER )
private Set<Employee> employees = new LinkedHashSet<Employee>();
}


Employee {
@ManyToOne
@JoinColumn( name = "DEPARTMENT_ID" )
private Department departmentId;
}


I have created a DepartmentDTO which I would like to return to the client. But I receive a stack overflow. It recursively calls the objects.



These are the 2 mappers I have created:
@Mapper(componentModel = "spring")
public interface DepartmentMapper {

List<DepartmentDTO> departmentList, @Context CycleAvoidingMappingContext cycleAvoidingMappingContext);


DepartmentDTO toDepartmentDto(Department department, @Context CycleAvoidingMappingContext cycleAvoidingMappingContext);

}

@Mapper(componentModel = "spring")
public interface EmployeeMapper {

List<EmployeeDTO> employeeList, @Context CycleAvoidingMappingContext cycleAvoidingMappingContext);


EmployeeDTO toEmployeeDto(Employee employee, @Context CycleAvoidingMappingContext cycleAvoidingMappingContext);

}


public class CycleAvoidingMappingContext {

private final Map<Object, Object> knownInstances = new IdentityHashMap<>();

@BeforeMapping
public <T> T getMappedInstance(Object source, @TargetType Class<T> targetType) {
return targetType.cast( knownInstances.get(source) );
}

@BeforeMapping
public void storeMappedInstance(Object source, @MappingTarget Object target) {
knownInstances.put( source, target );
}

}


Inside the service I call the mapper like this:


List<DepartmentDTO> departmentDtoList = listFromDB, new CycleAvoidingMappingContext() );


I would like to send as a response a list of DepartmentDTO which might contain a list of EmployeeDTOs inside of it. But instead I get an infinite loop and then I get a stack overflow. What am I doing incorrectly?

Thank you in advance!

ainigma
Автор

Tanks for the video. MapStruct is getting bigger and stronger as well as its community.
Besides, I don't speak french but I can see, someone has had an issue with recursive calls :D So have I. Using @Context CycleAvoidingMappingContext from someone's github-repo as well as @JsonIgnoreProperties ("field name as in callee class") solved my problem. But obviously there's been no best practice, yet, hasn't there?

dmitrikonnov