I'm using JPA Entity Graphs to eagerly load relationships.
But, my problem is:
The relation is an auto-relationship like this:
public class X{
...
@OneToOne
@JoinColumn(name = "foreign_key")
protected @Getter @Setter X parent;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private @Getter @Setter Set<X> children;
}
So, when I get the object X from the database, the children come loaded too. This is right. But, when I get the first object of children Set and get their children, hibernate do execute another query in the database.
My entity class is anotated with this:
@NamedEntityGraph(
name = "cobranca-com-lancamentos-com-filhos",
attributeNodes = {
@NamedAttributeNode(value = "children")
}
)
and my search is like this:
em.createQuery("FROM X WHERE id = " + id).setHint("javax.persistence.fetchgraph", em.getEntityGraph("cobranca-com-lancamentos-com-filhos")).getSingleResult();
Any idea?