简体   繁体   English

使用 jpa 规范检索有限列

[英]Retrieving limited columns using jpa specifications

I am using spring boot JPA specifications for executing complex queries.我正在使用 spring 引导 JPA 规范来执行复杂的查询。 However, the table I am querying contains more than 20 columns and I need to pull just 3. I tried cq.multiselect(...) but it didn't work and returned me the entity with all the columns.但是,我正在查询的表包含 20 多列,我只需要提取 3 列。我尝试cq.multiselect(...)但它不起作用并返回了包含所有列的实体。

On investigation, I got to know that it's a bug with specifications that's not yet fixed.经过调查,我知道这是一个尚未修复的规范错误。 Another option was to use projections but specifications can't be combined with projections.另一种选择是使用投影,但规格不能与投影结合。 An attempt to do so returns the complete entity.尝试这样做会返回完整的实体。

I do not want to switch to Querydsl or @Query approach since it's an existing code and I am stuck with specifications.我不想切换到 Querydsl 或 @Query 方法,因为它是现有代码,而且我受制于规范。 Any pointers on how to limit the number of columns will be much appreciated:)任何有关如何限制列数的指针将不胜感激:)

Use QueryDslPredicateExecutor.findAll(Predicate predicate, Pageable pageable) and then get the actual results back using PageRequest.of(0, limit).使用 QueryDslPredicateExecutor.findAll(Predicate predicate, Pageable pageable) 然后使用 PageRequest.of(0, limit) 获取实际结果。

Querydsl limit record example Querydsl 限制记录示例

Got to know Spring boot JPA Specifications doesn't provide any way to limit the number of columns.了解 Spring 启动 JPA 规范没有提供任何限制列数的方法。 There is a bug related to this feature and it's pending for more than 3 years now.有一个与此功能相关的错误,现在已经有 3 年多的时间了。 Not sure if it will be available soon不确定它是否会很快上市

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM