DeludedPsyche
DeludedPsyche

Reputation: 129

JPA: Search for an associated entity in an entity hierarchy

I've a PARENT Entity which has 1-M associations with other child table entities. Those child entities may further have associations with other table entities. In my application we load the PARENT entity and subsequent child entities also get loaded along with it.
Example:

[PARENT]
 -X
 --X1
 --X2
 ---X2a
 -Y
 --Y1
 --Y1
 -Z
 --Z1

I am stuck in an implementation where I have to search for a particular child entity object which could be anywhere in PARENT entity's hierarchy; update that child entity and save the Parent object.

E.g.: PARENT > X2 > X2a is to be updated.

Problem is there is no certain level of hierarchy i've to search through. I did try to implement JPA Metamodel and reflection api but couldnt find anything which could help me search for the entity in the loaded PARENT entity.

Can someone guide me to right way?

Upvotes: 1

Views: 371

Answers (1)

DeludedPsyche
DeludedPsyche

Reputation: 129

Implementation DONE. My objective was to get the child entity reference irrespective of the level it is in Parent entity model.

I simply loaded the child entity directly and since i had maintained the associations @OneToMany and @ManyToOne/@JoinColumn in Parent and child entities respectively; the whole entity model was loaded including the parent entities.

I only had to write my own logic to iterate through the entity looking for the parent most entity which i achieved using reflection checking each field of the entities with @ManyToOne/@JoinColumn.

Thought to conclude this problem scenario so that anyone else could get the tip.

Thanks

Upvotes: 1

Related Questions