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!