如何在 Spring Boot 中动态获取 EntityGraph

How to fetch EntityGraph dynamically in Spring Boot

我正在开发一个应用程序 Spring Boot using JPA。 在应用程序中,我公开了一个休息 API。我不想使用 Spring 数据剩余,因为我想完全控制数据。

我不知道如何动态使用 EntityGraph。

假设我有以下来自 here

的模型
   @Entity
class Product {

  @ManyToMany
  Set<Tag> tags;

  // other properties omitted
}

interface ProductRepository extends Repository<Customer, Long> {

  @EntityGraph(attributePaths = {"tags"})
  Product findOneById(Long id);
}

我有以下休息 link 来访问产品 http://localhost:8090/product/1

它 returns 对我来说是一个 ID 为 1

的产品

问题:

  1. 它会像我们提到的@EntityGraph 那样默认获取 tags 吗? 如果是,那么可以按需配置吗?说,如果在查询中 string 我有 include=tags,然后我只想获取产品 它的标签。

我找到了 this 篇文章,但不确定这有什么帮助。

Spring Data JPA Repository 中 EntityGraph 的定义是静态的。如果你想让它动态化,你需要像在你链接到的页面中一样以编程方式执行此操作:

EntityGraph<Product> graph = this.em.createEntityGraph(Product.class);
graph.addAttributeNodes("tags"); //here you can add or not the tags

Map<String, Object> hints = new HashMap<String, Object>();
hints.put("javax.persistence.loadgraph", graph);

this.em.find(Product.class, orderId, hints);

您还可以使用 JPA 存储库中的 EntityGraph 定义方法。

interface ProductRepository extends Repository<Product, Long> {

@EntityGraph(attributePaths = {"tags"})
@Query("SELECT p FROM Product p WHERE p.id=:id")
Product findOneByIdWithEntityGraphTags(@Param("id") Long id);
}

然后在您的服务中使用此方法与 EntityGraph 或内置 findOne(T id) 而不使用 EntityGraph:

Product findOneById(Long id, boolean withTags){
  if(withTags){
    return productRepository.findOneByIdWithEntityGraphTags(id);
  } else {
    return productRepository.findOne(id);
  }
}

您可以在存储库中执行此操作:

interface ProductRepository extends Repository<Product, Long> {

    Product findOneById(Long id);

    @EntityGraph(attributePaths = {"tags"})
    Product findOneWithTagsById(Long id);
}

并按照 Robert Niestroj 的建议创建服务方法。

您可以在运行时使用 Spring Data JPA EntityGraph 选择 EntityGraph。
设置非常简单:

  • 添加:implementation 'com.cosium.spring.data:spring-data-jpa-entity-graph:2.0.7' 到 build.gradle
  • @SpringBootApplication
  • 下方添加:@EnableJpaRepositories(repositoryFactoryBeanClass = EntityGraphJpaRepositoryFactoryBean.class)

现在,您可以在运行时选择最佳的 EntityGraph。示例(这是来自 Spring Data JPA EntityGraph 的示例):

// This will apply 'Product.brand' named EntityGraph to findByLabel
productRepository.findByLabel("foo", EntityGraphs.named("Product.brand"));

// This will apply 'Product.supplier' named EntityGraph to findByLabel
productRepository.findByLabel("foo", EntityGraphs.named("Product.supplier"));

// This will apply 'supplier' attribute paths EntityGraph (don't need to define named EntityGraph) to findByLabel
productRepository.findByLabel("foo", EntityGraphUtils.fromAttributePaths("supplier"));

请阅读文档以获取更多信息。

您可以如下添加实体图,确保实体产品class与标签class有关系。

@EntityGraph(attributePaths = {
        "tags"
    })
@Query( value = "select product from product)
List<Product> findAllProduct();