gpt4 book ai didi

java - 具有连接实体属性的 Micronaut Data DTO 投影

转载 作者:行者123 更新时间:2023-11-30 05:28:11 27 4
gpt4 key购买 nike

我将 Micronaut Data 与 JPA 结合使用,并有两个实体。第一个是食谱:

@Entity
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

private String name;

@ManyToOne
private Category category;

@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<Step> steps;

// + other fields, getters and setters
}

第二个是ParseError,它引用了Recipe:

@Entity
@Table(name = "parse_error")
public class ParseError implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY)
private Recipe recipe;

@Id
@Enumerated(EnumType.ORDINAL)
@Column(name = "problem_area")
private ProblemArea problemArea;

private String message;

// + other fields, getters and setters
}

现在我想在 API 中提供具有 ParseError 属性的 DTO,但不提供整个 Recipe 实体,因为它包含在本例中不需要的 ManyToOne 和 OneToMany 关系。所以我为此创建了投影 DTO:

@Introspected
public class ParseErrorDto {
private Integer recipeId;

private String recipeName;

private ParseError.ProblemArea problemArea;

private String message;

// + getters and setters
}

并将 listAll() 方法添加到 ParseErrorRepository 中:

@Repository
public interface ParseErrorRepository extends CrudRepository<ParseError, Integer> {
List<ParseErrorDto> listAll();
}

但 Micronaut Data 似乎无法从嵌套实体投影属性,或者我错过了 DTO 或存储库方法中的某些内容:

ParseErrorRepository.java:22: error: Unable to implement Repository method: ParseErrorRepository.listAll(). Property recipeId is not present in entity: ParseError

我还尝试创建RecipeDto:

@Introspected
public class RecipeDto {
private Integer id;

private String name;

// + getters and setters
}

并相应地更新了 ParseErrorDto:

@Introspected
public class ParseErrorDto {
private RecipeDto recipe;

private ParseError.ProblemArea problemArea;

private String message;

// + getters and setters
}

再次没有成功:

ParseErrorRepository.java:22: error: Unable to implement Repository method: ParseErrorRepository.listAll(). Property [recipe] of type [RecipeDto] is not compatible with equivalent property declared in entity: ParseError

Micronaut Data 是否能够通过 DTO 投影处理此用例?如果没有,那么还有其他方法如何在 Micronaut Data 中解决它吗?

最佳答案

现在(在最新版本 1.0.0.M1 中)这是不可能的。所以我为此创建了功能请求问题:https://github.com/micronaut-projects/micronaut-data/issues/184

当前的解决方法是将实体 bean 映射到 Java 流或 react 流中的 DTO bean,并手动或通过 Mapstruct 进行属性映射。

<小时/>

更新:以下是评论中问题的答案,并举例说明了如何使用 Mapstruct 进行解决方法:

将 Mapstruct 依赖项添加到 build.gradle 中:

implementation "org.mapstruct:mapstruct:$mapstructVersion"
annotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"
testAnnotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"

定义映射器:

import org.mapstruct.Mapper;

@Mapper(
componentModel = "jsr330"
)
public interface ParseErrorMapper {
ParseErrorDto entityToDto(@NotNull ParseError parseError);

EntityReference recipeToDto(@NotNull Recipe recipe);
}

这是该映射器在 Controller 中的用法:

@Controller("/parse-error")
public class ParseErrorController {
private final ParseErrorRepository repository;
private final ParseErrorMapper mapper;

public ParseErrorController(ParseErrorRepository repository, ParseErrorMapper mapper) {
this.repository = repository;
this.mapper = mapper;
}

@Get("all")
@Transactional
public Page<ParseErrorDto> getAll(final Pageable pageable) {
return repository.findAll(pageable).map(mapper::entityToDto);
}
}

关于java - 具有连接实体属性的 Micronaut Data DTO 投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58144514/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com