by BehindJava

What is Association Mapping and examples of One to One, Many to Many and One to Many and Many to One relationships

Home » java » What is Association Mapping and examples of One to One, Many to Many and One to Many and Many to One relationships

In this tutorial we are going to learn about Association Mapping and examples of One to One, Many to Many and One to Many and Many to One relationships.

Association Mapping

When we develop huge enterprise applications, we don’t just put all the data into one single database table like Order. We normalize our database tables into separate tables to hold the appropriate data like Order, Product and Address and each of these tables will have relationships with other tables usually through primary keys and foreign keys.

This is where association mapping comes in, to map these database tables to our domain classes or entities the JPA entities along with their associations for which we need JPA association mapping.

Relationships:

One to One

Example: A Person has only one License

Many to Many

Example: An order can have multiple products and in the same way a product can be in multiple orders.

One to Many and Many to One

A customer can have multiple phone numbers and multiple phone numbers have a single customer Form the customers perspective it is one to many and from the phone numbers perspective its many to one.

These relationships occur in two modes in which we can configure them in Uni directional or bi directional.