如何在Spring Boot中动态获取EntityGraph

阿什瓦尼K

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

我无法弄清楚如何动态使用EntityGraph。

假设我有以下模型取自这里

   @Entity
class Product {

  @ManyToMany
  Set<Tag> tags;

  // other properties omitted
}

interface ProductRepository extends Repository<Customer, Long> {

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

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

它向我返回ID为1的产品

问题:

  1. 默认情况下是否会像我们提到的@EntityGraph那样获取标签如果是,那么可以按需配置吗?说,如果在查询字符串中我具有include = tags,那么只有我想获取带有其标签的产品。

我找到了这篇文章,但不确定如何提供帮助。

罗伯特·尼斯特罗伊

Spring Data JPA存储库中的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);
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Spring Boot Java中动态获取假客户端名称和URL

来自分类Dev

如何在主函数中获取ApplicationContext - Spring boot

来自分类Dev

如何在 Spring Boot 中获取 ResponseEntity 的值?

来自分类Dev

如何在Spring Boot中禁用ErrorPageFilter?

来自分类Dev

如何在Spring Boot中添加UrlRewriteFilter

来自分类Dev

如何在spring boot中查询关系?

来自分类Dev

如何在ServletContextListener中获取Spring ApplicationContext?

来自分类Dev

如何在 Spring boot 中向 JMSListener Annotation 动态添加不同的目的地?

来自分类Dev

如何在Spring Boot中覆盖Spring Security默认配置

来自分类Dev

如何在Spring Boot中替换Spring ApplicationContext

来自分类Dev

如何在Android中动态获取ViewId

来自分类Dev

如何在JavaScript中获取动态ID

来自分类Dev

如何在Firebase中动态获取密钥?

来自分类Dev

如何在dataweave中获取动态列

来自分类Dev

如何在Firebase中动态获取密钥?

来自分类Dev

如何在Spring Boot Filter中获取HTTP请求正文内容?

来自分类Dev

如何在httpd proxypass后面的EL spring-boot页面中获取原始请求URI

来自分类Dev

如何在Spring Boot 1.3的Interceptor preHandle方法中获取方法信息

来自分类Dev

如何在Spring Boot Response中获取没有转义字符的JSON?

来自分类Dev

如何在Java Spring Boot中从请求标头获取承载令牌?

来自分类Dev

如何在 spring-boot-starter-data-redis 中获取 Jedis 实例?

来自分类Dev

如何在Spring DSL中动态设置CamelContext ID?

来自分类Dev

如何在MongoDB中的Spring数据中使用动态模式?

来自分类Dev

如何在 Spring 框架中定义动态模型

来自分类Dev

如何在 Spring 中动态启用系统文件访问?

来自分类Dev

如何在Spring Boot中使用CommonsMultipartResolver

来自分类Dev

如何在 Spring Boot 上生成 GeoJson?

来自分类Dev

如何在 Spring Boot 上返回 JSONObject?

来自分类Dev

如何在Spring Boot中创建Tcp Connection以接受连接?

Related 相关文章

  1. 1

    如何在Spring Boot Java中动态获取假客户端名称和URL

  2. 2

    如何在主函数中获取ApplicationContext - Spring boot

  3. 3

    如何在 Spring Boot 中获取 ResponseEntity 的值?

  4. 4

    如何在Spring Boot中禁用ErrorPageFilter?

  5. 5

    如何在Spring Boot中添加UrlRewriteFilter

  6. 6

    如何在spring boot中查询关系?

  7. 7

    如何在ServletContextListener中获取Spring ApplicationContext?

  8. 8

    如何在 Spring boot 中向 JMSListener Annotation 动态添加不同的目的地?

  9. 9

    如何在Spring Boot中覆盖Spring Security默认配置

  10. 10

    如何在Spring Boot中替换Spring ApplicationContext

  11. 11

    如何在Android中动态获取ViewId

  12. 12

    如何在JavaScript中获取动态ID

  13. 13

    如何在Firebase中动态获取密钥?

  14. 14

    如何在dataweave中获取动态列

  15. 15

    如何在Firebase中动态获取密钥?

  16. 16

    如何在Spring Boot Filter中获取HTTP请求正文内容?

  17. 17

    如何在httpd proxypass后面的EL spring-boot页面中获取原始请求URI

  18. 18

    如何在Spring Boot 1.3的Interceptor preHandle方法中获取方法信息

  19. 19

    如何在Spring Boot Response中获取没有转义字符的JSON?

  20. 20

    如何在Java Spring Boot中从请求标头获取承载令牌?

  21. 21

    如何在 spring-boot-starter-data-redis 中获取 Jedis 实例?

  22. 22

    如何在Spring DSL中动态设置CamelContext ID?

  23. 23

    如何在MongoDB中的Spring数据中使用动态模式?

  24. 24

    如何在 Spring 框架中定义动态模型

  25. 25

    如何在 Spring 中动态启用系统文件访问?

  26. 26

    如何在Spring Boot中使用CommonsMultipartResolver

  27. 27

    如何在 Spring Boot 上生成 GeoJson?

  28. 28

    如何在 Spring Boot 上返回 JSONObject?

  29. 29

    如何在Spring Boot中创建Tcp Connection以接受连接?

热门标签

归档