Skip to content
Advertisement

Hibernate: How to distinguish two uni-directional relationship and one bi-directional relationship?

in this answer, the author said:

Take an example of two entities mapped without declaring a owning side:

@Entity
@Table(name="PERSONS")
public class Person {
    @OneToMany
    private List<IdDocument>  idDocuments;
}

@Entity
@Table(name="ID_DOCUMENTS")
public class IdDocument {
    @ManyToOne
    private Person person;
}

From a OO point of view this mapping defines not one bi-directional relation, but two separate uni-directional relations.

1.My first question is: Why it is not a bi-directional relation?

Docs Oracle:

In a bidirectional relationship, each entity has a relationship field or property that refers to the other entity. Through the relationship field or property, an entity class’s code can access its related object

in the above code, both class have a relationship field that refers to the other entity. then why it is not a bi-directional relation and these are two uni-directional relations?

2.My second question is: what is the difference between two uni-directional relations and one bi-directional relation? aren’t they same thing?

Advertisement

Answer

(1) No ideas why the author said it is not a bi-directional relationship.

From the hibernate documentation , it also mentions that a similar mapping is a bidirectional which both involved entities can navigate to each other.

In this example, given a Person , we can get its IdDocument by its idDocuments field. And given an IdDocument , we can get its Person by its person field. So it is a bi-directional.

(2) I am not sure too. To me, whenever both entities in a relationship can navigate to each other , it is a bi-directional relationship. If only one entity can navigate to another but not vice versa , it is a unidirectional relationship.

And you can find the equivalent unidirectional case of the example the I mentioned above in this for comparing their differences.

Advertisement