简体   繁体   中英

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

You can create an of method within your TopicDTO class to construct the object. Providing the userDTO and categoryDTO as arguements will allow them to be set to null or their respective objects if they exist.

class TopicDTO {

    String header;

    String description;

    //all the other variables...

    public static TopicDTO of(Topic topic, UserDTO userDTO, CategoryDTO categoryDTO) {
    return new TopicDTO(
            topic.getHeader(),
            topic.getDescription(),
            topic.getIsAnonymous(),
            topic.getCreationDate(),
            userDTO,
            categoryDTO);
    }
}

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();
    }
}

Влад, you should use constructors to create objects only. Do not use getters/setters when instantiating the object because this is anti pattern. It's hard to maintain what setter you have used already and what not. Object should be fully created through constructor and key word 'new' and it will have state immediately.

To map Entity to dto I would suggest create simple Mapper class that accept Entity in constructor and return new dto object and that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM