Modify SQL query generated behind Spring Data REST projections(修改 Spring Data REST 投影后面生成的 SQL 查询)
问题描述
如何在 SELECT 中只保留 Spring Data Rest Projections 所需的列?
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 中的情况下,如何创建 Projections
How can Projections be created where also SQL queries have only those columns in SELECT which are in Projection
推荐答案
我不知道为什么文档中缺少它,但是这个 spring 示例(来自 spring)显示您可以使用投影作为 @ 的返回类型询问.所以你可以这样做:
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-
参考: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 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:修改 Spring Data REST 投影后面生成的 SQL 查询
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01