Good day,
I have a schema as follows
A -> one to many -> B
B-> one to many -> C
what I am trying to do is get all the grandchildren ( entity C ) from the parent ( entity A ). I have searched for a while and got to this point on class A
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name="B",
joinColumns= @JoinColumn(name="a_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="b_id", referencedColumnName="id")
)
@JsonManagedReference
public List<C> getC() {
return C;
}
however when the entity A is called I always get duplicates with the same number of entries I have inside the database so if I have 1 C item and I add a new C item and try to get the grandchildren from A I will be getting the same C item twice
I am not sure what I am doing wrong here or if there is any other possibility of getting C from A in another way?
EDIT
based on @SternK comment I updated my code to be as follows which works however I feel it can be improved a little, any advise?
in class C repo i added a function which accepts an ID from class A and with 2 fetch joins as follows
@Query("Select p FROM C c join fetch c.b cb join fetch cb.a cba where cba.a_id = :aId")
List<C> getC(@Param("aId") UUID aId);
and switched the property to become transient and in the service I loop over the list
of A
Entities and use this function to populate the property however I feel this can be improved ? or is this the best I can get out of it?
note that with this approach I think am getting hitting the database too much based on the amount of queries I see in the debugger.