【问题标题】:Spring Data Projections. Recursive problems春季数据预测。递归问题
【发布时间】:2018-04-18 09:31:05
【问题描述】:

我对递归投影有疑问。

在 Spring Data Rest 中使用投影的最佳方法是什么?

我有下一个实体:ProfileParamsChapters

它们之间的关系如下:

-> 使用参数配置 ManyToMany

-> 带有章节的参数 ManyToOne

我已经定义了我的个人预测并将其包含在我的存储库中,并带有 excerpt 属性,但是当我请求它们时,投影仅在第一级有效,在我的情况下,当我列出配置文件时,我看到的投影我申请了配置文件实体,但没有看到我在第二级为 param 实体定义的投影。

预测在这里:

@Projection(name = "profileProjection", types = Profile.class)
public interface ProfileProjection {

    int getId();

    List<Param> getParams();

}

@Projection(name = "paramProjection", types = Param.class)
public interface ParamProjection {

    int getId();

    Chapter getChapter();

    List<Profile> getProfiles();

}

此外,当我只想查看第一级时,我会递归地查看与我的参数关联的配置文件。停止递归的最佳方法是什么?我尝试在这样的 url 中使用 projection param,但效果不佳。

....../parametros?projection=parametroProjection

是否可以在一个 url 中应用两个投影?

【问题讨论】:

    标签: spring spring-data spring-data-jpa spring-data-rest


    【解决方案1】:

    我不确定你在问什么。如果您想组合投影,您可以执行以下操作:

    @Projection(name = "profileProjection", types = Profile.class)
    public interface ProfileProjection {
        int getId();
    
        // note type of list is ParamProjection 
        List<ParamProjection> getParams(); 
    }
    
    @Projection(name = "paramProjection", types = Param.class)
    public interface ParamProjection {
    
        int getId();
        Chapter getChapter();
    }
    

    ....../parametros?projection=profileProjection

    【讨论】:

    • 谢谢!这是解决方案,我在所有其他投影中都使用相同的投影,正确的用法是为每个其他投影创建特定的投影。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2016-09-07
    • 2015-07-25
    • 2011-10-19
    • 2018-08-01
    相关资源
    最近更新 更多