修改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
,其具有字段foo
, bar
, zed
。
您应该为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查询中的性能?
- 如何在MySQL中创建临时过程?
- 书:现代相当于Wrox Spring的书
- Spring 3 MVC Hibernate 3.5.4 hibernateTemplate没有关闭连接(非事务性)
- AES / CBC / PKCS5Padding与AES / CBC / PKCS7Padding具有256个密钥大小的性能java
- 在sbt / play-framework中包含/排除资产文件夹/目录
- Eclipse – 完成一个程序
- Field.get(obj)返回注入的CDI托管bean上的所有空值,而手动调用getter则返回正确的值
- 如何在Eclipse中设置SplashScreen?
- 在对象中实现二进制搜索