Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need the third table for many to many mapping ? Why can't I just use two tables?

I have been trying many to many relationship between two classes Person and Address. I don't know but somewhere I am not thinking along the correct lines. For example for many to many mapping, I made two tables

CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id));
CREATE TABLE address(a_id INTEGER,address TEXT);

and then I tried something in the mapping xml. After some unsuccessful attempts I read that you need three tables for many to many mapping, just as an answer to one of my question says.

Please explain me the reason for this ? Why do I need the third table ? Why cannot I make an association just with the two tables ?

like image 762
Suhail Gupta Avatar asked Jun 25 '13 08:06

Suhail Gupta


People also ask

Why do we need a third table for many-to-many relationships?

This relationship cannot be modeled using two tables. Each of these mappings would have a one side, meaning one of the particular entities corresponds with many of the other. None of these options achieve the many to many relationship and the use of a third table is required to map the more complex relationship.

How do you join two tables with many-to-many relationships?

When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.

How many tables are there in a many-to-many relationship?

Connect the three tables to create the many-to-many relationship. To complete the many-to-many relationship, create a one-to-many relationship between the primary key field in each table and the matching field in the intermediate table.

What is the problem with many-to-many relationship?

Many to Many(M:N) Relationship Many to many relationships create uncertainty and duplications on data that will eventually result in wrong statements for queries(2). In the below example; Each person can use many banks and each bank can have many customers.


3 Answers

The third table serves as a junction table that defines the many to many relationship. In your example I assume that a Person can have multiple addresses and an address can belong to multiple People. This relationship cannot be modeled using two tables.

You may attempt to simply include a foreign key to the Address in the Person table or a foreign key to Person in the Address table. Each of these mappings would have a one side, meaning one of the particular entities corresponds with many of the other. None of these options achieve the many to many relationship and the use of a third table is required to map the more complex relationship.

In order to map as a many to many you need to be able to associate multiple instances of both entities with each other. This is traditionally done via the following:

Table A
ID_A

Table B
ID_B

Table C
ID_A
ID_B
like image 153
Kevin Bowersox Avatar answered Oct 16 '22 21:10

Kevin Bowersox


Because of the nature of the relation.

If the mapping was one-to-one, than you could add a person_id column to the Address table and each Address tuple would point to just one Person.

 Address
+---------------------+
|id|p_id|address      |
+---------------------+
| 1|  1 |some street 1| //one address uniquely points to one person
+---------------------+
| 2|  2 |new street 5 | 
+---------------------+

Same goes for one-to-many: if a Person can have multiple Addresses, then there would be multiple tuples in Address table with the same person_id.

 Address
+---------------------+
|id|p_id|address      |
+---------------------+
| 1|  1 |some street 1| //two addresses point to one person
+---------------------+
| 2|  1 |new street 5 | 
+---------------------+

But what if one Person can have mutliple Addresses, but also, one Address can belong to multiple Persons? Then one column person_id in the Address table would not be enough because one Address can relate to many Persons! So you need a third table to associate all pairs of Persons and Addresses.

 Assoc table
+---------+
|a_id|p_id|    
+---------+
| 1  |  1 | //one address for two persons
+---------+
| 1  |  2 | 
+---------+
| 2  |  3 | //two addresses for the same person
+---------+
| 3  |  3 | 
+---------+
like image 38
darijan Avatar answered Oct 16 '22 23:10

darijan


Hmmm I'm not sure but I think he's talking about creating a "third class" in his mapping and not really "third table" (otherwise he wouldn't even talk about hibernate mapping). So I will assume that he's just struggling with his mapping for my answer (and it will still be useful later for his hibernate mapping) :

Thanks to the @JoinTable annotation you don't have to create an entity for the third table. If you have a many-to-many relationship between your tables, you just have to create 2 entities "Person" and "Address", and the @JoinTable annotation on each side will apply the many-to-many magic without having to create an entity for the third table between them.

EXCEPT if your third table have some extra columns, and then in that case you don't have a choice you will have to create a specific entity for this third table (or otherwise you can't get that extra column with just "Person" and "Address" entities).

Some useful links for your mapping :

http://www.mkyong.com/hibernate/hibernate-many-to-many-relationship-example-annotation/ http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

like image 42
Jérôme Sengel Avatar answered Oct 16 '22 22:10

Jérôme Sengel