Mapping of child classs

1,880 views
Skip to first unread message

amit sharma

unread,
Mar 8, 2016, 10:16:45 AM3/8/16
to mapstruct-users
I have source  class Employee contains reference to base class Student(it is not abstract).  There are two sub classes for Student  ScienceStudent and CommerceStduent

This Student reference in Employee  can point to either ScienceStduent class object or CommerceStudet class object .

Similarly I have Target  DTO Employee with Student reference. I am not understanding how map struct at run time would determine whether Student refer  to ScienceStudent or CommerceStduent.

I provided the mappping between student objects and also child objects but it is not working because it is just showing the field exist in Base student class not child class properties in result.

Andreas Gudian

unread,
Mar 8, 2016, 11:28:55 AM3/8/16
to amit sharma, mapstru...@googlegroups.com
If you are unsure what MapStruct generated mappers do at runtime, just check the generated code... :)
We currently don't support any instanceof-checking and downcasting to known child classes, so you'd have to implement that method on your own, somehow along those lines:

@Mapper
public abstract class EmployeeMapper {
  // impelementation to be generated by MapStruct
  public abstract EmployeeDto toEmployeeDto(Employee employee);
  protected abstract ScienceStudentDto toScienceStudentDto(ScienceStudent s);

  // manually implemented helper method
  protected StudentDto toStudentDto(Student student) {
    if ( student instanceof ScienceStudent ) {
      return toScienceStudentDto( (ScienceStudent) student );
    else if (student instanceof CommerceStudent ) {
      ..
    }
    return null;
  }
}



Hope it helps,
Andreas

--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

amit sharma

unread,
Mar 8, 2016, 11:48:42 AM3/8/16
to mapstruct-users, resol...@gmail.com
This for the response what if I have some thing like this 
Target
=========
Employee{
StudentArrayClass student

}

source
=====
Employee{
Student[] student

}

StudentArrayClass{

List<Student> studentList
}


We have child classes  ScienceStudent and CommerceStudent of Student.

Amit Sharma

unread,
Mar 8, 2016, 11:56:21 AM3/8/16
to Andreas Gudian, mapstru...@googlegroups.com
Thank you so much for the response. I have few other thing 

This for the response what if I have some thing like this 
Target
=========
Employee{
StudentArrayClass student

}

source
=====
Employee{
Student[] student

}

StudentArrayClass{

List<Student> studentList
}


We have child classes  ScienceStudent and CommerceStudent of Student.

I converted the Student[] to  Student List  and then List to StudentArrayClass and the used your code to map child classes. 


Reply all
Reply to author
Forward
0 new messages