Self join with head id field

I have a self-join table with a head table and I am using Spring JPA with Hibernate.

My table B entity is like that:

public class Table B {


   List<TableB> child;
   TableB parent;

   TableA topParent;


I usually get errors because the TableB sub-line is usually lost the TableA value. Of course, I should set TableA value in every TableB object but do I have any annotation or solution to automatically ensure the TableA will not miss?

No, there is no other way. You will just have to set the value on every object.