0

I read about ModelMapper, today and it seems to be very interesting, but I'm not sure about the right usage.

I have a Spring-Project like this: I have my model classes which are necessary for serialization. My REST controller return DTO-objects to the front end. And my frontend returns DTOs to my controller and then I need my model objects from the DTOs to write it to the database.

I have a person class that has an attribute like: Set<Company> companies = new HashSet<Company>();

I want the modelmapper to map this set to an attribute: Set<String> companies = new HashSet<String>().The 2nd set shall be filled by calling companies.getName() instead of filling the Set with the whole object.

My questions:

  1. Do I need a PropertyMap or a converter?
  2. How exactly can I do this?
  3. Is it possible to say Convert from Set<Companies> into a single String. Like I want just one company?

Sorry, I'm very new to ModelMapper and I'm searching for the best way to map during serialization and deserialization in combinatino with spring.

AndreasGloeckner
  • 292
  • 3
  • 5
  • 18

1 Answers1

0

If the name of fields are same in both dto and bean then we can use Spring's BeanUtils class to convert the objects, as shown below:

private UserDto toDto(User user) {
    UserDto dto = new UserDto();
    BeanUtils.copyProperties(user, dto, new String[]{"companies"});
    if (user.getCompanies() != null) {
        //Iterate the list and set the company names
    }
    return dto;
}

BeanUtils belongs to org.springframework.beans package so no dependency is needed. We can pass the array of properties to be ignored as an argument in copyProperties method (like companies in our case) if we want to handle those by ourselves. It uses Reflections and invokes getters and setters to set the values.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Where are the differences between BeanUtils and ModelMapper except an additional dependency? So i need to write my own matching method. I especially need help with deserialization. So when i have a company name (String) to convert it back to a company object. – AndreasGloeckner Feb 29 '16 at 20:34
  • How are we converting company name to company object? Does it need a database call? – Darshan Mehta Feb 29 '16 at 20:56
  • I don't get what your problem is Sorry... I want to save my person object to database later. For that I need same structure as my model has. I want to write the complete person object (table: person) with a List of companies (table companies) into the database. in this case I cant write my DTO to database because it has not a company attribute. – AndreasGloeckner Feb 29 '16 at 21:07
  • I don't have any problem mate :) I just wanted to say that, we may need a database call (e.g. findByName or something) to convert company name into company object. In such scenario, it is better to use converter design pattern (http://programmers.stackexchange.com/questions/185343/design-pattern-for-object-conversion-java) and use BeanUtils to convert simple fields. – Darshan Mehta Feb 29 '16 at 22:20
  • Ok, I actually hoped to avoid querying against the database again because that means that I need to send an unique key of every class to frontend before. When I get an answer by the frontend I need to call the database to get the rest of the data... :/ I will have a closer look at this pattern. Ty mate. – AndreasGloeckner Feb 29 '16 at 22:37