×
☰ See All Chapters

Association mapping in JPA

DB designing team designs the tables of the project according to “normalization rules”. There are six normalization rules/forms. The second normalization rule says design the tables having integrity constraints, means tables should be designed having relations like many-to-one, one-to-many, one-to-one, many-to-many. When tables are in association/relationship we can accesses one table data based on another table data, because records of one table represents the records of another table. DB team takes the support of primary key and foreign key constraint to design the tables having relationships.

When two DB tables are in relationship their JPA persistence classes must be designed and configured supporting that relationship, this work is called “Association Mapping”. When persistence classes are designed supporting relationship then the objects of these classes are actually in object level relationship/association.

Citizen-Passport

one-to-one

One Citizen contains only one Passport

User-Phone Number

one-to-many

One user can have many phone Numbers

Phone Number- User

many-to-one

Many phone Numbers belongs to one user.

Employee-Department

many-to-one

Many Employees belongs to one Department

Department- Employee

one-to-many

One Department can have many Employees.

Student-Course

many-to-many

One Student can do many Course, One Course can have many Students

Project-Employee

many-to-many

One Project can has many Employees, One Employee will be involved in many Projects

 

Using Phone Number and User we can have both one-to-many, many-to-one relationship, but generally Phone Number doesn’t need User Information, only User need Phone Number Information. (User class has Phone Number, Phone Number class need not to have User).

Using Employee-Department we can have both one-to-many, many-to-one relationship, here Employee and Department both may need information of other, so we create 12M bidirectional association mapping, because one-to-many bidirectional association mapping and M21 bidirectional association mapping are same.

To define one-to-one, many-to-one, one-to-many two tables are enough (parent, child) but to define many-to-many association three tables are required (table1, table2, relationship table).

Object level relationship

 

Unidirectional

Bidirectional

one-to-one

Yes

Yes

one-to-many

Yes

Yes

many-to-one

Yes

Yes

many-to-many

No

Yes

 

By using parent persistence class object of parent table we are able to access the associated child class object/objects data of child table, and if reverse is not possible then it is called Unidirectional association. If reverse is also possible then it is called Bidirectional association.

Parent persistence class => Persistence class for Parent table (table having PRIMARY KEY)

Child persistence class => Persistence class for Child table (table having FOREIGN KEY)

The JPA programmer should always design persistence classes based on the E-R diagrams (Entity-Relationship Diagram) given by DB Team.

To perform association mapping, persistence class should follow below conditions:

Persistence classes should have a property whose type should be of other persistence class, for example User class should have a property of type Phone Number. In persistence class we use following annotations:  ManyToMany, OneToMany, ManyToOne, OneToOne

 associations-in-jpa-0
 

In the above tables between User and Phone Number, tables are designed such that one User has Many Phone Numbers, so between User and Phone Number we should use 12M bidirectional association mapping(One user has many phone numbers and many phone numbers belongs to one user). But generally user needs phone number information and phone number doesn’t need user information hence we use 12M unidirectional association mapping.

associations-in-jpa-1
 

In the above tables between User and Phone Number, tables are designed such that one User record has only one record in Phone Number table. This is also may be the chance that Database tables are designed. Here we should use 121 Bidirectional association mapping, but as said earlier phone number doesn’t need user information, hence generally we use 121 unidirectional association mapping. The JPA programmer should always design persistence classes based on the E-R diagrams (Entity-Relationship Diagram) given by DB team. Use eclipse support to generate the persistence classes, use JPA project. While generating persistence classes using eclipse we should have ready tables in Database. To generate fine persistence classes just add some sample records in Database tables.


All Chapters
Author