<bdo id='vqHwA'></bdo><ul id='vqHwA'></ul>
  • <tfoot id='vqHwA'></tfoot>

    <i id='vqHwA'><tr id='vqHwA'><dt id='vqHwA'><q id='vqHwA'><span id='vqHwA'><b id='vqHwA'><form id='vqHwA'><ins id='vqHwA'></ins><ul id='vqHwA'></ul><sub id='vqHwA'></sub></form><legend id='vqHwA'></legend><bdo id='vqHwA'><pre id='vqHwA'><center id='vqHwA'></center></pre></bdo></b><th id='vqHwA'></th></span></q></dt></tr></i><div id='vqHwA'><tfoot id='vqHwA'></tfoot><dl id='vqHwA'><fieldset id='vqHwA'></fieldset></dl></div>

      <small id='vqHwA'></small><noframes id='vqHwA'>

      1. <legend id='vqHwA'><style id='vqHwA'><dir id='vqHwA'><q id='vqHwA'></q></dir></style></legend>
      2. 指定是否延迟加载 Spring Data

        时间:2024-08-24

                  <bdo id='bxwq3'></bdo><ul id='bxwq3'></ul>

                  <small id='bxwq3'></small><noframes id='bxwq3'>

                    <tbody id='bxwq3'></tbody>
                • <tfoot id='bxwq3'></tfoot>
                • <i id='bxwq3'><tr id='bxwq3'><dt id='bxwq3'><q id='bxwq3'><span id='bxwq3'><b id='bxwq3'><form id='bxwq3'><ins id='bxwq3'></ins><ul id='bxwq3'></ul><sub id='bxwq3'></sub></form><legend id='bxwq3'></legend><bdo id='bxwq3'><pre id='bxwq3'><center id='bxwq3'></center></pre></bdo></b><th id='bxwq3'></th></span></q></dt></tr></i><div id='bxwq3'><tfoot id='bxwq3'></tfoot><dl id='bxwq3'><fieldset id='bxwq3'></fieldset></dl></div>

                  <legend id='bxwq3'><style id='bxwq3'><dir id='bxwq3'><q id='bxwq3'></q></dir></style></legend>
                • 本文介绍了指定是否延迟加载 Spring Data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在实体中有一个惰性获取类型集合.我正在使用 Spring Data (JpaRepository) 来访问实体.

                  @Entity公共类家长{@ID私人长ID;@OneToMany(mappedBy = "parentId", fetch = FetchType.LAZY)私人套装<孩子>孩子们;}

                  我想要服务类中的两个函数,当前实现如下:

                  1. children"在获取父级时应该为空

                    public Parent getParent(Long parentId){返回 repo.findOne(parentId);}

                  2. children"取parent时要填写:

                     public Parent getParentWithChildren(Long parentId){父 p = repo.findOne(parentId);Hibernate.initialize(p.children);返回 p;}

                  当从 RestController 返回父"实体时,抛出以下异常:

                  @RequestMapping("/parent/{parentId}")public Parent getParent(@PathVariable("parentId") Long id){Parent p= parentService.getParent(id);//到这里为止return p;//转换为JSON时抛出的错误}

                  <块引用>

                  org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:未能延迟初始化集合角色:com.entity.Parent.children,无法初始化代理 - 否会话(通过引用链:com.entity.Parent["children"]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException: 懒惰失败初始化角色集合:com.entity.Parent.children,不能初始化代理 - 无会话(通过引用链:com.entity.Parent["children"])

                  解决方案

                  如果您希望根据用例允许同一域模型的不同 JSON 表示,那么您可以查看以下内容,您可以这样做所以不需要 DTO:

                  https://spring.io/博客/2014/12/02/latest-jackson-integration-improvements-in-spring

                  或者,另请参阅下面的Spring Data REST 中的投影"部分

                  https://spring.io/blog/2014/05/21/what-s-new-in-spring-data-dijkstra#projections-in-spring-data-rest

                  I have a lazy fetch type collection in an entity. And I am using Spring Data (JpaRepository) for accessing the entities.

                  @Entity
                  public class Parent{
                  @Id
                  private Long id;
                  
                      @OneToMany(mappedBy = "parentId", fetch = FetchType.LAZY)
                      private Set<Child> children;
                  }
                  

                  I want two functions in service class and current implementation are as following:

                  1. "children" should be null when fetching parent

                    public Parent getParent(Long parentId){
                        return repo.findOne(parentId);
                    }
                    

                  2. "children" should be filled when fetching parent:

                     public Parent getParentWithChildren(Long parentId){
                         Parent p = repo.findOne(parentId);
                         Hibernate.initialize(p.children);
                         return p;
                    }
                    

                  When returning "Parent" entity from a RestController, following exception is thrown:

                  @RequestMapping("/parent/{parentId}")
                  public Parent getParent(@PathVariable("parentId") Long id)
                  {
                      Parent p= parentService.getParent(id);//ok till here
                      return p;//error thrown when converting to JSON
                  }
                  

                  org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: com.entity.Parent.children, could not initialize proxy - no Session (through reference chain: com.entity.Parent["children"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.entity.Parent.children, could not initialize proxy - no Session (through reference chain: com.entity.Parent["children"])

                  解决方案

                  If you are looking to allow for different JSON representations of the same domain model depending on use case, then you can look at the following which will allow you to do so without requiring DTOs:

                  https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring

                  Alternatively, see also the 'Projections in Spring Data REST' section in the following

                  https://spring.io/blog/2014/05/21/what-s-new-in-spring-data-dijkstra#projections-in-spring-data-rest

                  这篇关于指定是否延迟加载 Spring Data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SpringData:查询注释中是否可以有子查询? 下一篇:在mongodb中查询一个文档及其所有匹配条件的子文档(使用spring)

                  相关文章

                    <bdo id='DZEQe'></bdo><ul id='DZEQe'></ul>
                • <i id='DZEQe'><tr id='DZEQe'><dt id='DZEQe'><q id='DZEQe'><span id='DZEQe'><b id='DZEQe'><form id='DZEQe'><ins id='DZEQe'></ins><ul id='DZEQe'></ul><sub id='DZEQe'></sub></form><legend id='DZEQe'></legend><bdo id='DZEQe'><pre id='DZEQe'><center id='DZEQe'></center></pre></bdo></b><th id='DZEQe'></th></span></q></dt></tr></i><div id='DZEQe'><tfoot id='DZEQe'></tfoot><dl id='DZEQe'><fieldset id='DZEQe'></fieldset></dl></div>
                  1. <tfoot id='DZEQe'></tfoot>

                    1. <legend id='DZEQe'><style id='DZEQe'><dir id='DZEQe'><q id='DZEQe'></q></dir></style></legend>

                    2. <small id='DZEQe'></small><noframes id='DZEQe'>