How to convert entity to dto, and vice versa?
10:00 11 Apr 2022

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

java spring spring-boot jpa data-transfer-objects