修改Spring Data REST投影背后生成的SQL查询

编辑:如何只在SELECT中保留所需的列以进行Spring Data Rest Projections?

Spring Data Rest Projections适用于为生成的链接获取列的子集,但在后面生成的Query仍然包含其中的所有列。

如何创建预测,其中SQL查询只有SELECT中的那些列在Projection中

我不知道为什么文档中缺少它,但是这个春季样本(来自spring)表明你可以使用projection作为@Query的返回类型。 所以你可以这样做:

public interface ActionId { String getId(); } @Query("select a.id as id from Action a where a.type = :type") public List findByType(@Param("type") String type); 

现在,您可以更简洁地选择所需的列,并返回一个对象,而不必使用构造函数表达式。 我希望这些预测可以应用于域对象本身,因此您仍然可以返回仅包含id字段的“Action”,但现在看起来不可能 –

参考: https : //github.com/spring-projects/spring-data-examples/blob/master/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java

我想你可以使用“嵌套投影”。

示例:资源A包含字段bubi和资源B ,其具有字段foobarzed

您应该为B创建一个投影,只列出您想要的字段:

 @Projection(name="reduced", types = B.class) public interface BReduced { String foo; //exclude bar, for instance int zed; } 

然后在A的投影中使用该投影。

 @Projection(name="reduced", types = A.class) public interface AReduced { int bubi; BReduced b; } 

我是否感觉不好,或者您是在谈论SQL查询中的性能?