我嘗試將 dto 映射到避免復雜物件的物體,但是當我嘗試保存我的文章物體時出現空例外?在我的 ArticleRequest 而不是使用 Department & ArticleCategory 作為一個完整的物件,我只是把他們的 ids (uid) 。
這是我的文章請求:
@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.getArticleCategoryUid()); // i have null exeption here !! because ArticleCategory already null
article.getDepartment().setUid(articleRequest.getDepartmentUid()); // i have null exeption here !! because Department already null
return article;
}
}
這是我的 baseEntity
@Data
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity implements Serializable {
//region Simple Properties
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long id;
@NotNull
@Size(min = 32, message = "uid should have at least 32 characters")
@Column(name = "uid", nullable = true,updatable = false)
private String uid;
//endregion
}
這是我的文章類別類
@Data
@Entity
@Table(name = "article_categories", schema = "public")
public class ArticleCategory extends BaseEntity {
//region Simple Properties
@NotNull
@Size(min = 2, message = "name should have at least 2 characters")
@Column(name = "name")
private String name;
@NotNull
@Size(min = 2, message = "slug should have at least 2 characters")
@Column(name = "slug")
private String slug;
@NotNull
@Size(min = 2, message = "description should have at least 2 characters")
@Column(name = "description")
private String description;
//endregion
//region Complex Properties
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "articleCategory", orphanRemoval = true)
private List<Article> articleList;
//endregion}
這是我的系課
@Data
@Entity
@Table(name = "departments", schema = "public")
public class Department extends BaseEntity {
//region Simple Properties
@Size(min = 2,message = "name should have at least 2 characters")
@Column(name = "name")
private String name;
@Enumerated(EnumType.STRING)
@Column(name = "status")
private DepartmentStatusEnum status;
//endregion
//region Complex Properties
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "department", orphanRemoval = true)
private List<Article> articleList;
}
這是我的服務
@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;
}
那么我如何才能避免空例外并以正確的方式傳遞文章類別和部門的 uid!
提前致謝。
uj5u.com熱心網友回復:
給出的資訊不是一個好主意,你應該在你的問題中通過課堂部門和文章
可能你的問題在這里:
article.getArticleCategory().setUid(articleRequest.getUid()); article.getDepartment().setUid(articleRequest.getUid());
您應該設定文章類別和文章的部門,創建一個新物件并設定它們。我認為解決方案是將這些行替換為:
article.setArticleCategory(new ArticleCategory());
article.getArticleCategory().setUid(articleRequest.getArticleCategoryUid());
article.setDepartment(new Department());
article.getDepartment().setUid(articleRequest.getDepartmentUid());
uj5u.com熱心網友回復:
article.getArticleCategory() //this gives you the NPE
在呼叫它的 getter 方法之前初始化 articleCategory。
例如:
article.setArticleCategory(new ArticleCategory());
部門也一樣。呼叫前初始化部門物件getDepartment()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/377905.html
