修改Spring Data REST投影背后生成的SQL查询 [英] Modify SQL query generated behind Spring Data REST projections

查看:134
本文介绍了修改Spring Data REST投影背后生成的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:
如何在SELECT数据库中仅保留所需的列以进行Spring Data Rest预测?

EDIT : How to Keep only needed columns in SELECT for Spring Data Rest Projections?

Spring Data Rest Projections如下:有利于为生成的链接获取列的子集,但在后面生成的查询仍然包含其中的所有列。

Spring Data Rest Projections are good for getting a subset of columns for links which are generated, but the Query that gets generated in behind still has all columns in it.

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

How can Projections be created where also SQL queries have only those columns in SELECT which are in Projection

推荐答案

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

I don't know why it's missing from the docs, but this spring sample (from spring) shows that you can use projections as the return type for a @Query. So you can do:

public interface ActionId {
    String getId(); 
}

@Query("select a.id as id from Action a where a.type = :type")
public List<ActionId> findByType(@Param("type") String type);

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

Now instead of having to use constructor expressions, you can more succinctly just select the columns you want, and return an object. I wish that projections could be applied to the domain object itself, so you could still return an "Action" with just the id field, but that doesn't look possible now-

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

这篇关于修改Spring Data REST投影背后生成的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆