I am working on JPA/Hibernate entities with JpaRepositories. I have few entities with @OneToOne
and @OneToMany
relationships. To make code short and simple I have named entities as A, B, C, D etc.. A is the root entity and below chain there is Inheritance with Joined strategy. Please look code below -
A.java
@Entity
@Table(name = "A")
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long aId;
@Column
private String name;
@OneToOne
@JoinColumn(name = "bId")
private B b;
@OneToMany(mappedBy = "a")
private Set<C> cSet;
// other entity specific fields
// getters and setters
}
B.java
@Entity
@Table(name = "B")
public class B {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long bId;
// other entity specific fields
// getters and setters
}
C.java
@Entity
@Table(name = "C")
public class C {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long cId;
@ManyToOne
@JoinColumn(name = "aId", referencedColumnName = "aId")
private A a;
@OneToMany(mappedBy = "c")
private Set<D> dSet;
// other entity specific fields
// getters and setters
}
D.java
@Entity
@Table(name = "D")
@Inheritance(strategy = InheritanceType.JOINED)
public class D {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long dId;
@ManyToOne
@JoinColumn(name = "cId", referencedColumnName = "cId")
private C c;
@OneToMany(mappedBy = "d")
private Set<E> eSet;
// other entity specific fields
// getters and setters
}
E.java
@Entity
@Table(name = "E")
public class E {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long eId;
@ManyToOne
@JoinColumn(name = "dId", referencedColumnName = "dId")
private D d;
// other entity specific fields
// getters and setters
}
F.java
@Entity
@Table(name = "F")
public class F extends D {
@OneToMany(mappedBy = "d")
private Set<H> hSet;
// other entity specific fields
// getters and setters
}
G.java
@Entity
@Table(name = "G")
public class G extends D {
@OneToMany(mappedBy = "d")
private Set<I> iSet;
// other entity specific fields
// getters and setters
}
H.java
@Entity
@Table(name = "H")
public class H {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long hId;
@ManyToOne
@JoinColumn(name = "dId", referencedColumnName = "dId")
private D d;
// other entity specific fields
// getters and setters
}
I.java
@Entity
@Table(name = "I")
public class I {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long iId;
@ManyToOne
@JoinColumn(name = "dId", referencedColumnName = "dId")
private D d;
// other entity specific fields
// getters and setters
}
If you see inheritance starts from entity D being parent, G and F extends D. I need to fetch all entities either by EnityGraph or Join fetch. All associations are lazy. I have id of the root A. I need to fetch complete data without n+1 queries for A (aId) when I call ... aRepository.findById(aId);
I tried many solutions but i am unable to fetch. The problem to create EntityGraph is the inheritance in between entities chain. The only way i was able to fetch was when I made associations EAGER. I don't want to make @OneToMany
mappings as EAGER.
Can anyone suggest me a solution (either @EntityGraph
or Join Fetch) to perform this task ?