如何使用 JHipster 在 mapstruct 中映射对象引用?
How to map object references in mapstruct using JHipster?
假设您使用这样的 JDL 脚本为具有 Posts 的博客创建了一个 JHipster 应用程序,并且您希望拥有一个显示其中 Posts 的 BlogDTO(以及一个显示每个 Post 的评论的 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 创建您的博客、Post 和评论实体,并假设您不想使用 Post 或 Post 填充博客带有评论,因此您的 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 将显示 Posts(并且 PostDTO 将显示评论)。谢谢
PD:因为我更改了注释以包含 PostMapper class
@Mapper(componentModel = "spring", uses = {PostMapper.class})
和 @Mapping(target = "posts", ignore = false) 为 FALSE 但它不起作用。 API 示例 (Swagger) 看起来不错,但是 PostDTO 为空(即使数据存在)。
向您的 BlogDTO 添加一个 Set<PostDTO> posts;
,向您的 PostDTO 添加一个 Set<CommentDTO> comments;
。还要为 DTO 文件中的那些字段添加 getter 和 setter。然后在您的映射器中,确保 BlogMapper 使用 PostMapper 并且 PostMapper 使用 CommentMapper。
您可能还需要在 Blog.java 中的 posts
字段和 Post.java 中的 comments
字段上配置缓存注释以适合您的用例。使用 NONSTRICT_READ_WRITE
,更新缓存可能会有延迟,导致 API.
返回陈旧数据
假设您使用这样的 JDL 脚本为具有 Posts 的博客创建了一个 JHipster 应用程序,并且您希望拥有一个显示其中 Posts 的 BlogDTO(以及一个显示每个 Post 的评论的 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 创建您的博客、Post 和评论实体,并假设您不想使用 Post 或 Post 填充博客带有评论,因此您的 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 将显示 Posts(并且 PostDTO 将显示评论)。谢谢
PD:因为我更改了注释以包含 PostMapper class @Mapper(componentModel = "spring", uses = {PostMapper.class})
和 @Mapping(target = "posts", ignore = false) 为 FALSE 但它不起作用。 API 示例 (Swagger) 看起来不错,但是 PostDTO 为空(即使数据存在)。
向您的 BlogDTO 添加一个 Set<PostDTO> posts;
,向您的 PostDTO 添加一个 Set<CommentDTO> comments;
。还要为 DTO 文件中的那些字段添加 getter 和 setter。然后在您的映射器中,确保 BlogMapper 使用 PostMapper 并且 PostMapper 使用 CommentMapper。
您可能还需要在 Blog.java 中的 posts
字段和 Post.java 中的 comments
字段上配置缓存注释以适合您的用例。使用 NONSTRICT_READ_WRITE
,更新缓存可能会有延迟,导致 API.