spring - 为什么 Spring Data JPA 和 QueryDSL 中会使用重复的变量?

标签 spring spring-data spring-data-jpa querydsl

网上有很多使用 QueryDSL 的例子,如下所示:

public class CustomerRepositoryImpl
    extends QueryDslRepositorySupport
    implements CustomerRepositoryCustom {

 public Iterable<Customer> findAllLongtermCustomersWithBirthday() {
    QCustomer customer = QCustomer.customer;

    return from(customer)
       .where(hasBirthday().and(isLongTermCustomer()))
       .list(customer);
    }
}

这段代码是有道理的,但我想知道为什么 customer 在对 list() 的方法调用中是“重复的”。

from(customer)中的引用中,类型不应该是显而易见的吗?

最佳答案

from 定义源并list 投影。 Querydsl 查询类没有任何返回类型的泛型类型参数,投影(选择部分)在查询构造链的最后一部分中定义。

需要与源不同的投影的情况示例

  • 仅限特定列:query.list(customer.firstName, customer.lastName)
  • 构造函数调用:Projections.constructor(...)
  • Bean 数量:Projections.bean(...)
  • 使用了多个 from 调用

关于spring - 为什么 Spring Data JPA 和 QueryDSL 中会使用重复的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533491/

相关文章:

java - MongoPersistentEntityIndexResolver - 在类型中找到字段 'variable' 的循环

java - 如何在 Spring Boot 中使用 Spring Validation 来验证嵌套对象?

java - 如何在applicationContext中从Spring中排除一个类?

spring-data - spring data如何根据方法名生成sql?

java - Spring 测试中未调用方面

java - spring-data-aerospike `findAll(ids)` 结果返回 NULL

java - 如何在 REST Spring 中正确处理具有相互对象引用的请求主体?

java - 创建名为 'requestMappingHandlerAdapter' Spring Boot 的 bean 时出错

java - Spring 中的 JavaConfig 是什么?

java - Spring MVC 使用 STS 和 tomcat devloader