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 ?
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.
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.
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.
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.
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
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 Address
es, 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 Address
es, but also, one Address
can belong to multiple Person
s? Then one column person_id
in the Address
table would not be enough because one Address
can relate to many Person
s! So you need a third table to associate all pairs of Person
s and Address
es.
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 |
+---------+
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With