0

I have 2 entities:

public class Foo {

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "foo")
  @JsonManagedReference
  private List<Bar> bars;
}

public class Bar {

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn("foo_id")
  @JsonBackReference
  private Foo foo;
}

This works fine when I fetch the database, but because of the lazy loading it's quite slow when I want to send hundreds of Foos to the UI after mapping them into DTOs.

I've tried to solve this problem with an EntityGraph, but when I put the Bar list into the EntityGraph like this:

@NamedEntityGraph(
  name = "foo.graph",
  attributeNodes = {
    @NamedAttributeNode("bars")
  }
)
@Entity
public class Foo {...

And try to fetch using the EntityGraph, I get an infinite loop. I've tried many things, but nothing seems to work.

Any idea what am I missing?

Thanks!

Hidan
  • 1
  • Are you using the lombok's @Data or @EqualsAndHashCode? – SSK May 13 '20 at 13:51
  • Do you have custom toString ? – antoine.lange May 13 '20 at 13:55
  • Yes, I'm using Lombok's Data, but I tried with only Setter too, and added getters manually, and I made the Foo's getter in bar to return null, to break the cycle, but didn't help. I don't have custom toString, Lombok's data deals with that. – Hidan May 13 '20 at 17:21

0 Answers0