简体   繁体   English

JPA未获取数据引发stackoverflow错误

[英]JPA not fetching data throwing stackoverflow error

I have a very large json to store and I divided that into following five entities and trying to store to h2 database using spring-data-jpa,inserting is working fine into database, but while fetching A object with A primary key Id, I am getting stackoverflow error, because there is circular dependency. 我有一个非常大的json存储,我将其分为以下五个实体,并尝试使用spring-data-jpa存储到h2数据库,插入在数据库中工作正常,但是在获取具有主键ID的对象时,出现stackoverflow错误,因为存在循环依赖关系。

could someone help me to figure out what is the issue. 有人可以帮助我找出问题所在。

Top class 顶级

@Entity
@Table(name = "a")
@Data
public class A{

@Id
@Column(name = "a_id")
private String id;

@Column
private String name;

@Column
private String name2;

@Column
private String name3;

@Column
private String name4;

@Column
private String name5;

@OneToMany(cascade = CascadeType.ALL,mappedBy = "a",fetch = 
FetchType.EAGER)
@JsonManagedReference
private List<B> b;
}

Second class 二等

@Entity
@Table(name = "b")
@Data
public class B{

@Id
@Column(name = "bname")
private String bname;

@Column
private String bVersion;

@OneToMany(cascade = CascadeType.ALL,mappedBy = "b")
@JsonManagedReference
private List<C> cs;


@ManyToOne
@JoinColumn(name = "a_id")
@JsonBackReference
private A a;
}

Third class 三等

@Entity
@Data
public class C{

@Id
@Column(name = "cname")
private String cName;


@Column
private String cVersion;

@OneToMany(cascade = CascadeType.ALL,mappedBy = "c")
@JsonManagedReference
private List<D> ds;

@ManyToOne
@JoinColumn(name = "bname")
@JsonBackReference
private B b;
}

Fourth class 四等

@Entity
@Data
public class D{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="d_id")
private long id;

@Column
private String dName;

@Column
private String dName2;


@ElementCollection
@Column
private List<String> dNames;


@OneToMany(cascade = CascadeType.ALL,mappedBy = "d")
@JsonManagedReference
private List<E> e;

@ManyToOne
@JoinColumn(name = "cname")
@JsonBackReference
private C c;

}

Fifth class

@Entity
@Data
public class E{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "e_id")
private long id;

@Column
private String ename;

@Column
private String eName2;


@ManyToOne
@JoinColumn(name = "e_id")
@JsonBackReference
private E e;
}

I also faced the same problem and spent the whole day. 我也遇到了同样的问题,整整整整一天都在忙。 Finally, I found its problem with the toString method generated by Lombok using @Data annotation. 最后,我发现Lombok使用@Data注释生成的toString方法存在问题。

Solution: 解:

Override, toString method and write your own implementation. 覆盖toString方法并编写自己的实现。 If you do not want to use toString then use @Getter and @Setter instead of @Data . 如果您不想使用toString使用@Getter@Setter而不是@Data

Why 为什么

Note: This occurs with bi-directional relationship. 注意:这是双向关系。 Lets say I have two entity and having bi-directional relationship. 可以说我有两个实体并且具有双向关系。

@data
Entity1{
  @oneToMany
  public List<Entity2> entity2s;

  //Added by lombok while using @Data
  @Generated
    public String toString() {
        return "entity2s=" + this.getEntity2s() + ")";
    }
}

@data
Entity2{
  @ManytoOne
  public Entity1 entity1;


  //Added by lombok while using @Data
  @Generated
  public String toString() {
    return "entity1=" + this.getEntity1() + ")";
  }

}

In above example, toString()(called by JPA) of Entity1 calles toString() of Entity2 and toString() of Entity2 calls toString() of Entity1. 在上面的示例中,Entity1的toString()(由JPA调用)调用Entity2的toString() ,而Entity2的toString()调用Entity1的toString() And that is how circular reference is building. 这就是建立循环引用的方式。

Note: The same goes to hashCode method. 注意: hashCode方法也是如此。

Here is a some reference links, 这是一些参考链接,

https://github.com/rzwitserloot/lombok/issues/1007 https://github.com/rzwitserloot/lombok/issues/1007

https://interviewbubble.com/stackoverflow-tostring-method-when-using-lombok/ https://interviewbubble.com/stackoverflow-tostring-method-when-using-lombok/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM