亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具有連接實體屬性的 Micronaut Data DTO 投影

具有連接實體屬性的 Micronaut Data DTO 投影

人到中年有點甜 2023-09-06 16:55:47
我將 Micronaut Data 與 JPA 結合使用,并有兩個實體。第一個是Recipe:@Entitypublic 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:@Introspectedpublic class ParseErrorDto {    private Integer recipeId;    private String recipeName;    private ParseError.ProblemArea problemArea;    private String message;// + getters and setters}并將listAll()方法添加到ParseErrorRepository:@Repositorypublic interface ParseErrorRepository extends CrudRepository<ParseError, Integer> {    List<ParseErrorDto> listAll();}但似乎 Micronaut Data 無法從嵌套實體投影屬性,或者我錯過了 DTO 或存儲庫方法中的某些內容:ParseErrorRepository.java:22:錯誤:無法實現存儲庫方法:ParseErrorRepository.listAll()。實體中不存在屬性recipeId:ParseError我還嘗試創建RecipeDto:@Introspectedpublic class RecipeDto {    private Integer id;    private String name;    // + getters and setters}并相應更新ParseErrorDto:@Introspectedpublic class ParseErrorDto {    private RecipeDto recipe;    private ParseError.ProblemArea problemArea;    private String message;    // + getters and setters}再次沒有成功:ParseErrorRepository.java:22:錯誤:無法實現存儲庫方法:ParseErrorRepository.listAll()。[RecipeDto] 類型的屬性 [recipe] 與實體中聲明的等效屬性不兼容:ParseErrorMicronaut Data 是否能夠通過 DTO 投影來處理這個用例?如果沒有,那么我如何在 Micronaut Data 中解決它?
查看完整描述

1 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

當前的解決方法是將實體 bean 映射到 Java 流或反應流中的 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("/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);

? ? }

}


查看完整回答
反對 回復 2023-09-06
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號