Created
October 28, 2020 17:56
-
-
Save trevize8/577c1c3d5ed4f528bd0a65b38ff7f55e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When using @IterableMapping | |
... | |
@Override | |
public List<CompanyDTO> toDto(List<Company> company, Locale locale) { | |
// NO call to @BeforeMethod | |
if ( company == null ) { | |
return null; | |
} | |
List<CompanyDTO> list = new ArrayList<CompanyDTO>( company.size() ); | |
for ( Company company1 : company ) { | |
// here is using the method I need to be used, this is why I'm using @IterableMapping | |
list.add( toDto( company1, locale ) ); | |
} | |
return list; | |
} | |
... | |
// But If I remove it: | |
... | |
@Override | |
public List<CompanyDTO> toDto(List<Company> company, Locale locale) { | |
// Using the @Before method as expected | |
List<CompanyDTO> target = fixLazyLoadingList( company ); | |
if ( target != null ) { | |
return target; | |
} | |
if ( company == null ) { | |
return null; | |
} | |
List<CompanyDTO> list = new ArrayList<CompanyDTO>( company.size() ); | |
for ( Company company1 : company ) { | |
// here I'm missing the context param... | |
list.add( toDto( company1 ) ); | |
} | |
return list; | |
} | |
... | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############## | |
# Some beans # | |
############## | |
public class Company { | |
private String name; | |
private List<Employee> employees; | |
// getters and setters | |
} | |
public class CompanyDTO { | |
private String name; | |
private List<EmployeeDTO> employees; | |
// getters and setters | |
} | |
public class Employee { | |
private String name; | |
//getters and setters | |
} | |
public class EmployeeDTO { | |
private String name; | |
//getters and setters | |
} | |
################ | |
# Base Mappers # | |
################ | |
public interface BaseMapper<T, E> extends SessionAwareMapping <T,E>{ | |
T toDto (E entity); | |
E toEntity (T dto); | |
@Named("toDtoLocale") | |
T toDto(E entity, @Context Locale locale); | |
@Named("toEntityLocale") | |
E toEntity(T dto, @Context Locale locale); | |
@Named("toDtoListLocale") | |
List<T> toDto(List<E> entities, @Context Locale locale); | |
@Named("toEntityListLocale") | |
List<E> toEntity(List<T> dtos, @Context Locale locale); | |
} | |
public interface SessionAwareMapping <T,E> { | |
@BeforeMapping | |
default <T> List<T> fixLazyLoadingList(Collection<E> c) { | |
// some logic here. | |
} | |
} | |
################ | |
# Mappers # | |
################ | |
@Mapper (componentModel = "spring", uses = {EmployeeMapper.class}) | |
public interface CompanyMapper extends BaseMapper<CompanyDTO, Company>{ | |
@Named("toDtoLocale") | |
@Mapping(target="employees", qualifiedByName = "toDtoListLocale") | |
CompanyDTO toDto(Company company, @Context Locale locale); | |
// If I remove below annotation, then the @BeforeMapping method is being used | |
@IterableMapping(qualifiedByName = {"toDtoLocale"}) | |
List<CompanyDTO> toDto(List<Company> company, @Context Locale locale); | |
} | |
@Mapper(componentModel = "spring") | |
public interface EmployeeMapper extends BaseMapper<EmployeeDTO, Employee>{ | |
@Named("toDtoListLocale") | |
@IterableMapping(qualifiedByName = "toDtoLocale") | |
List<EmployeeDTO> toDto(List<Employee> entities, @Context Locale locale); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment