Micronaut 数据 DTO 投影与连接实体的属性

cgr*_*rim 5 java jpa micronaut micronaut-data

我将 Micronaut Data 与 JPA 一起使用,并且有两个实体。第一个是Recipe

@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
}
Run Code Online (Sandbox Code Playgroud)

第二个是ParseErrorRecipe

@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
}
Run Code Online (Sandbox Code Playgroud)

现在我想在 API 中为 DTO 提供ParseError属性而不是整个Recipe实体,因为它包含在这种情况下不需要的 ManyToOne 和 OneToMany 关系。所以我为此创建了投影 DTO:

@Introspected
public class ParseErrorDto {
    private Integer recipeId;

    private String recipeName;

    private ParseError.ProblemArea problemArea;

    private String message;

// + getters and setters
}
Run Code Online (Sandbox Code Playgroud)

并将listAll()方法添加到ParseErrorRepository

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

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

ParseErrorRepository.java:22:错误:无法实现存储库方法:ParseErrorRepository.listAll()。实体中不存在属性 recipeId:ParseError

我也尝试创建RecipeDto

@Introspected
public class RecipeDto {
    private Integer id;

    private String name;

    // + getters and setters
}
Run Code Online (Sandbox Code Playgroud)

并相应更新ParseErrorDto

@Introspected
public class ParseErrorDto {
    private RecipeDto recipe;

    private ParseError.ProblemArea problemArea;

    private String message;

    // + getters and setters
}
Run Code Online (Sandbox Code Playgroud)

再次没有成功:

ParseErrorRepository.java:22:错误:无法实现存储库方法:ParseErrorRepository.listAll()。[RecipeDto] 类型的属性 [recipe] 与实体中声明的等效属性不兼容:ParseError

Micronaut Data 能否通过 DTO 投影处理此用例?如果没有,那么还有另一种方法如何在 Micronaut Data 中解决它?

cgr*_*rim 4

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

当前的解决方法是将实体 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"
Run Code Online (Sandbox Code Playgroud)

定义映射器:

implementation "org.mapstruct:mapstruct:$mapstructVersion"
annotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"
testAnnotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"
Run Code Online (Sandbox Code Playgroud)

这是控制器中该映射器的用法:

import org.mapstruct.Mapper;

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

    EntityReference recipeToDto(@NotNull Recipe recipe);
}
Run Code Online (Sandbox Code Playgroud)

更新(2023 年 9 月):由https://github.com/micronaut-projects/micronaut-data/pull/2495实现的功能