Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring boot休眠无限获取循环_Java_Spring_Spring Boot_Hibernate_Jpa - Fatal编程技术网

Java Spring boot休眠无限获取循环

Java Spring boot休眠无限获取循环,java,spring,spring-boot,hibernate,jpa,Java,Spring,Spring Boot,Hibernate,Jpa,我做了一些研究,但没有任何具体的答案 我知道@JsonView@杰索尼诺的作品。。。但我在这里的观点是关于后端,从那里的观点。我正在使用spring boot,默认情况下OSIV是启用的,所以据我所知,如果我没有错的话,如果我在数据库中调用具有@manytomy关联的@Entity,它将急切地获取所有内容 在没有问题之前,问题是关联的集合也有集合。。。一些服务需要获取它们,而另一些则不需要。。。然后我不断地得到懒散的初始化异常 @Entity @EntityListeners(AuditingE

我做了一些研究,但没有任何具体的答案

我知道@JsonView@杰索尼诺的作品。。。但我在这里的观点是关于后端,从那里的观点。我正在使用spring boot,默认情况下OSIV是启用的,所以据我所知,如果我没有错的话,如果我在数据库中调用具有@manytomy关联的@Entity,它将急切地获取所有内容

在没有问题之前,问题是关联的集合也有集合。。。一些服务需要获取它们,而另一些则不需要。。。然后我不断地得到懒散的初始化异常

@Entity
@EntityListeners(AuditingEntityListener.class)
@Inheritance(strategy = InheritanceType.JOINED)
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, updatable = false)
    private int id;
    private String categoryTitle;
    private String categoryDescription;

    @ManyToMany
    @JoinTable(
            name = "Category_Parent",
            joinColumns = @JoinColumn(name = "id_category", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "id_category_parent")
    )
    private Set<Category> parentCategory;

    @ManyToMany
    @JoinTable(
            name = "Category_Parent",
            joinColumns = @JoinColumn(name = "id_category_parent", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "id_category")
    )
    private Set<Category> subCategory;
@实体
@EntityListeners(AuditingEntityListener.class)
@继承(策略=InheritanceType.JOINED)
公共类类别{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(nullable=false,Updateable=false)
私有int-id;
私有字符串分类;
私有字符串类别描述;
@许多
@可接合(
name=“Category\u Parent”,
joinColumns=@JoinColumn(name=“id\u category”,referencedColumnName=“id”),
inverseJoinColumns=@JoinColumn(name=“id\u category\u parent”)
)
私有集合父类别;
@许多
@可接合(
name=“Category\u Parent”,
joinColumns=@JoinColumn(name=“id\u category\u parent”,referencedColumnName=“id”),
inverseJoinColumns=@JoinColumn(name=“id\u类别”)
)
私有集子类别;
然后为了防止这个错误,我像这样使用@Query

@Repository
public interface CategoryRepository extends JpaRepository<Category, Integer> {
    @Query("from Category c left join fetch c.subCategory left join fetch c.parentCategory")
    List<Category> getAllCategories();
}
@存储库
公共接口类别repository扩展了JpaRepository{
@查询(“从类别c左连接获取c.subCategory左连接获取c.parentCategory”)
列出getAllCategories();
}
现在我可以轻松地获取它了……我使用了@Query方式,因为它是我所知道的唯一一个获取关联集合的方式……我听说了EntityGraph和Hibernate.initialize(),但不知道如何继续(希望有一些链接)

因此,我有Json异常,因为Json响应是无限的。我如何避免这个新问题?使用DTO

我很感激

----编辑---

我已经在我想作为响应发送的属性上使用了@JsonView,但是如果我只在子类别上使用@JsonView,它会起作用,但是如果我在parentCategory上使用@JsonView,我再次得到了无限循环…无法解决它。

  • 您可以将fetch=FetchType.LAZY添加到@manytomy或@OneToMany注释中,如下所示:@manytomy(fetch=FetchType.LAZY)。有关详细说明,请参阅

我已经试过了,但不起作用:/@TiagoVieira您能添加完整的异常堆栈跟踪吗?