mapping embedded object , need some help.

194 views
Skip to first unread message

dwi...@gmail.com

unread,
Dec 27, 2017, 2:09:36 PM12/27/17
to modelmapper
Hi,
I'm starting to learn java and trying to use modelmapper to convert beans to entities and vice versa in a spring mvc project. The classes (without Bean in the name) are hibernate entities f.ex. Applicant and I need to convert them to their corresponding bean (f.ex. ApplicantBean).

f.ex. I have the following classes:

class Country { some stuff }
class CountryBean { some stuff }

class Address { Country country }
class AddressBean { int countryId}


class Application {
 
Date applicationDate;
 etc
}

class ApplicationBean {
 
Date applicationDate;
 etc
}


class Applicant {
 
List<Application> applications;
 
String addressCity;
 
Country addressCountry;
 
String lastName;
 
String firstName;
  etc
}


class ApplicantBean {
 
List<ApplicationBean> applications;
 
AddressBean address;
 
String lastName;
 
String firstName;
 etc
}

I have to admit that I have problems to fully understand the modelmapper documentation.

How would you convert the entity class Applicant to an ApplicantBean with ModelMapper (and vice-versa)?

There are  a lot of embedded Objects and List<T> Objects etc ... so I'm kind of stuck ...

Thank you very much for helping! 
Kind regards, 
Didier

Chun Han Hsiao

unread,
May 1, 2018, 4:42:55 AM5/1/18
to modelmapper
Hi Didier,

For your case Application <-> ApplicationBean you can just do modelMapper.map(application, ApplicationBean.class) or modelMapper.map(applicationBean, Application.class).
I think there is no problem for map for these kind of classes.
It looks like you had some cases that had some generic types embedded in your classes. I think it need some more work to map each other correctly because it's always hard to know about the real type of the generic type.
So maybe you can provide more information about your cases, and I will try to provide you some solutions and explain how to use model mapper correctly.

Regards,
Chun Han

dwi...@gmail.com於 2017年12月28日星期四 UTC+8上午3時09分36秒寫道:
Reply all
Reply to author
Forward
0 new messages