Home > Net >  are "Unidirectional" and "Bidirectional" terms belong to OOP world or database w
are "Unidirectional" and "Bidirectional" terms belong to OOP world or database w

Time:04-23

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

I've question about "Unidirectional" and "Bidirectional" terms. when I search on internet, I see some posts and some articles that use these two terms for 'entity' and 'object'. so I assumed that these two words belong to OOP world.

ok. but in this video, the teacher (at 1:49) said:

everything in Database is is bi-directional. because based on Foreign key, you can reach the value from either side.

so it means that we can use Unidirectional and Bidirectional in database too. right? but I think the quote from the teacher is not right. because if we have tableA which has a foreign key to tableB and tableB does not have any foreign key to tableA, then it is Unidirectional because we cannot navigate and go from a tableB's record to a tableA's record.

Could you please help me with this issue? thank you

CodePudding user response:

I would agree that strictly speaking 'everything is bi-directional in the database' is not right. A relation between classes is bi-directional if A has a reference to B, and B has a reference to A. If a relation is uni-directional, one side does not even know about the existence of the other.

A foreign key reference is by definition in one direction: table A references table B. B does not know about A at all. In my opinion, it would be bi-directional if B would have a foreign key relation to A as well. The latter might in many cases be redundant or even unwanted, which you tend to get rid of in database design, but in principle exactly the same is true for relations between objects. "give me the parent with child X" gives the same result as "Child X give me your parent". If the relation is bi-directional, you can get an answer to these questions directly, you can navigate from both sides. But I can see that one might see it differently depending on how you interpret, or what you are trying to say with "bi-directional". I'm curious to read such an interpretation if anyone has one.

What is true is that a bi-directional one-to-many relation (many-to-one on the other side, e.g.: parent knows their children, children know their parent) with Hibernate will result in just a single foreign key relation in the database, since that is sufficient to resolve both sides of the relation. But I personally wouldn't call that bi-directional from a database perspective.

CodePudding user response:

let's say we have tableA with the below columns

  • id
  • col1

and tableB with below columns

  • id
  • col1
  • tableA_id (foreign key of tableA)

if tableA below data

 id | col1 
---- ------
  1 | A row1
  2 | A row2

and tableB below data

 id |  col1  | tablea_id 
---- -------- -----------
  2 | B row1 |         1
  3 | B row2 |         1
  4 | B row3 |         2
  5 | B row4 |         2

Now if we have tableA's id let's say 1 we can find all records for tableB using

select * from tableB where tablea_id = 1;

 id |  col1  | tablea_id 
---- -------- -----------
  2 | B row1 |         1
  3 | B row2 |         1

This way we can find both tables' records using a foreign key. Even JPA internally does the same things. You can see queries run by JPA by enabling query logs in the application.properties file using the below property

spring.jpa.show-sql=true

  • Related