【问题标题】:Can we make Spring JPA Specifications work with EntityGraph?我们可以使 Spring JPA 规范与 EntityGraph 一起使用吗?
【发布时间】:2020-04-07 20:07:09
【问题描述】:

我正在使用实体上的 JPA 规范实现动态查询逻辑。该实体与其他实体有多个关系。我在执行 JPA 规范时遇到了生成太多查询的问题。

有没有一种方法可以将 JPA 规范 findAll(specification, page) 与 EntityGraph 结合起来,以便我们可以在执行同一查询时生成一个查询?

【问题讨论】:

    标签: spring-boot jpa spring-data-jpa entitygraph


    【解决方案1】:

    是的,绝对可以使用EntityGraphJpaSpecificationExecutor 传递EntityGraph

    @Repository
    public interface UserRepository
            extends JpaRepository<EntityClassName, DatatypeOfPrimaryKey>, EntityGraphJpaSpecificationExecutor<EntityClassName> {
    }
    
    userRepository.findAll(specification, pageable, new NamedEntityGraph(EntityGraphType.FETCH, "graphName"))
    

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多