Search code examples
javaspringspring-bootjpadata-transfer-objects

How to convert entity to dto, and vice versa?


okay, i have an 3 entities: Topic, User, Category, Picture. User have a picture, and topic have an User and Category.

class Topic {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Integer id;

    @Column(nullable = false)
    String header;

    @Column(nullable = false)
    String description;

    @Column(name = "is_anonymous", nullable = false)
    boolean isAnonymous;

    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    @Column(name = "creation_date", nullable = false)
    LocalDateTime creationDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "author_id", nullable = false)
    User author;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", nullable = false)
    Category category;
}

And i also have an topic DTO

class TopicDTO {

    String header;

    String description;

    boolean isAnonymous;

    LocalDateTime creationDate;

    UserDTO userDTO;

    CategoryDTO categoryDTO;
}

I can to inject ModelMapper into TopicService, and use it to convert, but it doesn't work as I need, in this case, if i trying to convert Topic to TopicDTO, in the converted TopicDTO object, UserDTO and CategoryDTO will be null, but in the debug, before converting, in the Topic object - Category object and User object is not null, they are initialized.

I trying to write a CRUD services for each entities, into which i inject repositories that extends CrudRepository. And when i get from controller TopicDTO, i call topicService.save(topicDTO), but in the topic service, method save, i dont want to cascade save user, i dont want to cascade save categories, i want to save topic with existing samples category and user, how i can to do that? Sorry for my awful english


Solution

  • You can either use a code generator like MapStruct which I really don't preconise as you'll have to learn how to annotate your DTOs in order to map them and that it's quite deprecated. (For example it's testable only with junit4).

    You should rather use Lombok builders to instantiate DTOs from your entities. Futhermore you can test it easily with junit5 in TDD like this :

    class TopicMapperTest {
    
        TopicMapper topicMapper;
    
        @Mock
        UserMapper userMapper;
    
        Clock clock;
    
        @BeforeEach
        void setUp() {
            topicMapper = new TopicMapper();
            clock = Clock.fixed(LocalDateTime.now().toInstant());
        }
    
        @Test
        void should_map_topic_to_topicDTO() {
            // Given
            Topic topic = new Topic(1, "header", "description", false, LocalDateTime.now(clock), new User(), new Category());
    
            TopicDTO expected = TopicDTO.builder()
                    .header("header")
                    .description("description")
                    .isAnonymous(false)
                    .creationDate(LocalDateTime.of())
                    .userDTO(userMapper.mapUser(new User()))
                    .categoryDTO(categoryMapper.mapCategory(new Category()))
                    .build();
    
    
            // When
            TopicDTO result = topicMapper.mapTopic(topic);
    
            // Then
            assertEquals(expected, result);
        }
    }
    

    And your mapper should looks like (I let you complete it to make your test pass) :

    public class TopicMapper {
    
        UserMapper userMapper = new UserMapper();
    
        public TopicDTO mapTopic(Topic topic) {
            return TopicDTO.builder()
                    .header(topic.getHeader())
                    .description(topic.getDescription())
                    .userDTO(userMapper.mapUser(topic.getAuthor()))
                    .isAnonymous(topic.isAnonymous())
                    .build();
        }
    }