Suppose we have an Employee entity, this entity has a reflexive relation (relation with itself). The kind of the relation is @ManyToOne
mapping meaning, multiple Employee are supervised by only one "Boss" (who's also an Employee). Also Employee entity has relation with a Company entity.
Multiple Employee work for only one Company (@ManyToOne) and a Company know his boss (@OneToOne)
Question :
How can we persist this @ManyToOne and @OneToOne relation based on condition (criteria) ? We can assign (persist) a Boss to an Employee only if they work for the same company. And we can assign an Employee to a company only if he's the boss (means he hasn't supervisor (NULL)).
I guess there are many solution like using @Query, @Prepersist , @Filter, Do the checking in the service but how and which one is better for this case.