Mapping class names

25 views
Skip to first unread message

Hazer

unread,
Sep 28, 2017, 2:08:22 PM9/28/17
to modelmapper
If I have the following classes;

class Customer{
   
private String name;
   
private Address address;
   
List<Auto> autos;
   
// getters and setters
}
class Address {
   
private String street;
   
private String city;
   
private String zip;
   
// getters and setters
}
class Auto {
   
private String model;
   
private String make;
   
// getters and setters
}


And I mapping into the following classes (they are purposely identical except for classname to isolate the problem I am having)


class CustomerDTO {
   
private String name;
   
private AddressDTO addressDTO;
   
List<AutoDTO> autoDTOs;
   
// getters and setters
}
class AddressDTO {
   
private String street;
   
private String city;
   
private String zip;
   
// getters and setters
}
class AutoDTO {
   
private String model;
   
private String make;
   
// getters and setters
}

How would I go about configuring ModelMapper to handle the child objects with classnames that only differ by a suffix of DTO.  I am currently using the following configuration options, which will map all of the fields in Customer, but Address and List<Auto> arrive null every time.

modelMapper.getConfiguration().setSourceNameTokenizer(NameTokenizers.UNDERSCORE)
   
.setDestinationNameTokenizer(NameTokenizers.CAMEL_CASE)
   
.setFieldMatchingEnabled(true)

Thanks!

Hazer




Chun Han Hsiao

unread,
Dec 13, 2017, 2:15:56 AM12/13/17
to modelmapper
Hi Hazer,

I think it's not snake_case to camelCase problem.
It looks like your source and destination class are all in camelCases, so you didn't need to set the tokenizer strategy.
Please reference http://modelmapper.org/user-manual/configuration/#matching-strategies , and it looks like no matter if you are using LOOSE or STANDARD matching strategy, the ModelMapper won't know about the mapping between autos and audoDTOs.
So maybe you can try the property mapping feature http://modelmapper.org/user-manual/property-mapping/ for this case.

Regards,
Chun Han

Hazer於 2017年9月29日星期五 UTC+8上午2時08分22秒寫道:
Reply all
Reply to author
Forward
0 new messages