Home > Software design >  How to Map a dto to an entity with avoiding complexs objects
How to Map a dto to an entity with avoiding complexs objects

Time:12-09

I try to map dto to an entity with avoiding complex objects but I have a null exception when I try to save my article entity?? in my ArticleRequest instead of using Department & ArticleCategory as a full object I just put their ids (uid) .

Here is my ArticleRequest:

@Slf4j
@Builder
@Data
public class ArticleRequest {

    private Long id;

    @Size(min = 32, message = "uid should have at least 32 characters")
    private String uid;

    @Size(min = 2, message = "title should have at least 2 characters")
    private String title;

    @NotBlank(message = "content should not be empty value")
    private String content;

    @NotNull(message = "article category id should not be null value")
    private String articleCategoryUid;

    @NotNull(message = "department id should not be empty value")
    private String departmentUid;

    @JsonIgnore
    private List<ArticleVote> articleVoteList;

    public static ArticleRequest fromEntity(Article article) {
        if (article == null) {
            log.warn("Class: ArticleRequest || Method: fromEntity() || Error: article is null!!!");
            return null;
        }

        return ArticleRequest.builder()
                .id(article.getId())
                .uid(article.getUid())
                .title(article.getTitle())
                .content(article.getContent())
                .articleCategoryUid(article.getArticleCategory().getUid())
                .departmentUid(article.getDepartment().getUid())
                .build();

    }

    public static Article toEntity(ArticleRequest articleRequest) {
        if (articleRequest == null) {
            log.warn("Class: ArticleRequest || Method: toEntity() || Error: articleRequest is null!!!");
            return null;
        }

        Article article = new Article();
        article.setId(articleRequest.getId());
        article.setUid(articleRequest.getUid());
        article.setTitle(articleRequest.getTitle());
        article.setContent(articleRequest.getContent());
        article.getArticleCategory().setUid(articleRequest.getUid()); // i have null exeption here !! because ArticleCategory already null
         article.getDepartment().setUid(articleRequest.getUid()); // i have null exeption here !! because Department already null
        return article;
    }
}

and here is my service

@Override
    public ArticleRequest saveArticle(ArticleRequest articleRequest) {
        if (articleRequest == null) {
            throw new InvalidEntityException(CustomErrorMessage.ARTICLE_CAN_NOT_BE_NULL.getMessage(), CustomErrorCodes.ARTICLE_NOT_VALID);
        }
        articleRequest.setUid(UUID.randomUUID().toString());
        Article saveArticle=ArticleRequest.toEntity(articleRequest);// null exception
        Article newArticle = articleRepository.save(saveArticle);
        ArticleRequest newArticleRequest = ArticleRequest.fromEntity(newArticle);
        return newArticleRequest;
    }

so how I can avoid null exception & pass uid of articleCategory and department with the right way!

thanks in advance.

CodePudding user response:

Informations given dont give a great idea , you should past the class Department and Article in your question

Probably your problem is here :

article.getArticleCategory().setUid(articleRequest.getUid()); article.getDepartment().setUid(articleRequest.getUid());

You should set the ArticleCategory and the Department of your article , creating a new objtects and setting them . I think the solution is replacing these lines with :

article.setArticleCategory(new ArticleCategory());
article.getArticleCategory().setUid(articleRequest.getArticleCategoryUid()); 
article.setDepartment(new Department());
article.getDepartment().setUid(articleRequest.getDepartmentUid());

CodePudding user response:

article.getArticleCategory() //this gives you the NPE

Initialize articleCategory before you call the getter method of it.

E.g:

article.setArticleCategory(new ArticleCategory());

Its the same for department. Initialize department object before you call getDepartment()

  • Related