指定是否延迟加载 Spring Data

2022-01-18 00:00:00 spring java Hibernate jpa 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

相关文章