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:

@Entity
public class Table B {

   ....

   @OneToMany
   List<TableB> child;
   @ManyToOne
   TableB parent;

   @ManyToOne
   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.