mapstruct map an id to an object

Aymen Kanzari picture Aymen Kanzari · Feb 21, 2018 · Viewed 7.1k times · Source

i have an object school that has an object person, persons are already saved in a data base and when i save a school object i give it a person id

so in the class school i have an attribute person of type Person, and in SchoolDTO i have an attribute personId of type Long

 @Mapper(componentModel = "spring", uses = { PersonMapper.class }) 
 public interface SchoolMapper extends EntityMapper<SchoolDTO, School>{ 

  @Mapping(source = "personId", target = "person") 
  School toEntity(SchoolDTO schoolDTO); 
 } 

 School school = schoolMapper.toEntity(schoolDTO); 
 log.info(school.getPerson()); 


public interface EntityMapper <D, E> {

 E toEntity(D dto);

 D toDto(E entity);

 List <E> toEntity(List<D> dtoList);

 List <D> toDto(List<E> entityList);
}


@Mapper(componentModel = "spring", uses = {})
public interface PersonMapper extends EntityMapper<PersonDTO, Person> {

  default Person fromId(Long id) {
   if (id == null) {
     return null;
   }
   Person person= new Person();
   person.setId(id);
   return person;
  }
}

the problem here when i display the person it shows me the id with their value and the other attribute null

Answer

Filip picture Filip · Feb 25, 2018

The reason why your Person is displayed only with the id value set is because your fromId method creates an empty Person and sets only the id.

I presume you want to fetch the Person from the database.

To achieve this you just need to tell MapStruct to use a service, or you can inject it in your mapper and perform the fetch.

If you have a service like:

public interface PersonService {

    Person findById(Long id);
}

And your mapper:

 @Mapper(componentModel = "spring", uses = { PersonService.class }) 
 public interface SchoolMapper extends EntityMapper<SchoolDTO, School>{ 

  @Mapping(source = "personId", target = "person") 
  School toEntity(SchoolDTO schoolDTO); 
 }