【发布时间】:2019-01-04 18:21:34
【问题描述】:
假设您使用这样的 JDL 脚本为带有帖子的博客创建了一个 JHipster 应用程序,并且您想要一个显示其中的帖子的 BlogDTO(以及一个显示每个帖子的评论的 BlogDTO):
entity Blog {
creationDate Instant required
title String minlength(2) maxlength(100) required
}
entity Post {
creationDate Instant required
headline String minlength(2) maxlength(100) required
bodytext String minlength(2) maxlength(1000) required
image ImageBlob
}
entity Comment {
creationDate Instant required
commentText String minlength(2) maxlength(1000) required
}
// RELATIONSHIPS:
relationship OneToMany {
Blog to Post{blog required}
Post{comment} to Comment{post(headline) required}
}
// Set pagination options
paginate all with pagination
// DTOs for all
dto * with mapstruct
// Set service options to all except few
service all with serviceClass
// Filtering
filter *
Jhipster 将使用它们的 DTO 创建您的 Blog、Post 和 Comment 实体,并假设您不想用 Posts 填充 Blog 或用 cmets 填充 Posts,因此您的 BlogMapper 将如下所示:
@Mapper(componentModel = "spring", uses = {})
public interface BlogMapper extends EntityMapper<BlogDTO, Blog> {
@Mapping(target = "posts", ignore = true)
Blog toEntity(BlogDTO blogDTO);
default Blog fromId(Long id) {
if (id == null) {
return null;
}
Blog blog = new Blog();
blog.setId(id);
return blog;
}
}
使用这样的 BlogDTO:
public class BlogDTO implements Serializable {
private Long id;
@NotNull
private Instant creationDate;
@NotNull
@Size(min = 2, max = 100)
private String title;
//GETTERS, SETTERS, HASHCODE, EQUALS & TOSTRING
任何人都可以帮助修改代码,以便 BlogDTO 将显示帖子(而 PostDTO 将显示评论)。谢谢
PD:因为我更改了 Annotation 以包含 PostMapper 类 @Mapper(componentModel = "spring", uses = {PostMapper.class})
并且 @Mapping(target = "posts", ignore = false) 为 FALSE 但它不起作用。 API 示例 (Swagger) 看起来不错,但 PostDTO 为空(即使数据存在)。
【问题讨论】: