0

I'm trying to learn about JPA and Hibernate and I was trying to learn about some database terms.

In a video on youtube, the teacher (at 2:42) said:

One-To-Many is only Unidirectional.

and she said suppose thsese two class:

class Person {

    List<Address> addresses;

}


class Address {

    Person p;

}

and she said:

this is One-To-Many. but Address cannot have a collection of Person because if it has a collection of Person, it's going to be Many-To-Many.

but I think she is not right, because we can have these two:

thePerson.getAddresses().get(i);

and

anAddress.getPerson();

When we can have these two statements, then it is Bidirectional. Then why she said it can be just Unidirectional?

What is Bidirectional's exact definition with which she came to such a conclusion?

Harry Coder
  • 2,429
  • 2
  • 28
  • 32

2 Answers2

0

You are correct, she's not right. All kinds of relationships can de unidirectional or bidirectional.

On the child side, you have to annotate the field with @JoinColumn.

On the parent side, you have to use the property mappedBy of @OneToMany annotation.

Example:

@OneToMany(mappedBy = "user")
private List<Address> addresses;

@ManyToOne
@JoinColumn
private User user;

Here's a good lecture about it.

https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

0

First of all, any relationship can be unidirectional or bidirectional, it's all depend on how you want to display or retrieve your data, and your business rules.

When we can have these two statements, then it is Bidirectional. Then why she said it can be just Unidirectional?

There is also a big mismatch between the object oriented approach of ORMs like Hibernate and how tables and relationships are defined in a relational database. She can be wright, because, in the point of view of a database, relationships are by default unidirectional.

The difference between unidirectional and bidirectional is defined by the fact that you can access the records of the other side of your relationship from where you are.

What is Bidirectional's exact definition with which she came to such a conclusion?

In your example we can interpret your relationship like this:

  • Unidirectional: you can get all your addresses from person, but not the inverse

     class Person {
    
         @OneToMany
         List<Address> addresses;
    
     }
    
     class Address {
    
         // Here you don't add the ManyToOne
         Person p;
     }
    
  • Bidirectional: you can get all your addresses from person and get a person from an address

     class Person {
    
         @OneToMany
         List<Address> addresses;
     }
    
     class Address {
    
         @ManyToOne
         Person p;
     }
    

Take a look at those links below:

Difference between unidirectional and bidirectional relational relationship

https://www.baeldung.com/spring-data-rest-relationships

What is Object/Relational mismatch

Harry Coder
  • 2,429
  • 2
  • 28
  • 32
  • Harry when for those two class, we provide all of the Getters, then we have always bidirectional. right? –  Apr 23 '22 at 00:20
  • 1
    @TomWard no, as soon as you define an annotation in both classes, they are bidirectional. Those annotations should be taken in the point of view of Hibernate and Java. It helps the framework to know how to manage and retrieve your data. – Harry Coder Apr 23 '22 at 00:22
  • no I mean just in OOP world and without hibernate and jpa. if we provide all of the Getters, then we have always bidirectional. right? I'm confused. because with ```thePerson.getAddresses().get(i);``` and ```anAddress.getPerson();``` I see it is Bidirectional. just with providing Getters for both sides. –  Apr 23 '22 at 00:27
  • @TomWard yes in OOP world this also means you relationship is bidirectional. – Harry Coder Apr 23 '22 at 00:29