A Brief Introduction To Association Mappings In JPA

Posted By : Mayank Madhav | 31-May-2018

Association mapping is the key feature of JPA and Hibernate. They are used to establish an association between two domain models defined in the java class. It is similar to mapping between two tables in a relational database. There are 3 types of mapping in JPA as in relational databases : -

1. One to One Mapping.

2. Many to One Mapping.

3. Many to Many Mapping.

We can map between two models as uni-directional or bi-directional. 

One to One Mapping: -   One to One mapping means that there is exactly one instance which is related to each other. As for example, a customer has only one shipping address.

So, we can say that Customer and ShippingAddress are related by one to one mapping. Further One to One mapping can be uni-directional or bi-directional. 

Code for establishing uni-directional one to one mapping : ->

@Entity
public class Customer {
    @OneToOne
    @JoinColumn(name = "fk_shipping_address")
    private ShippingAddress shippingAddress;
}        

Code for establishing Bi-directional One to One Mapping:->

@Entity
public class Customer {
    @OneToOne
    @JoinColumn(name = "fk_shipping_address")
    private ShippingAddress shippingAddress;
}
@Entity
public class ShippingAddress {
    @onetoone 
    @JoinColumn(mappedBy="shippingAddress")
    private Customer customer;
}  

2. Many To One mapping : ->

Many to One relationship an entity can have many instances related to its one instance. for example, for a college, there may be many students but all the students belong to a single college. So, we can conclude by saying that between students and college there is many to one relationship.
Code to establish many to one relationship : ->

@Entity
public class Student {
    @ManyToOne
    @JoinColumn(name="fk_college")
    private College college;
}
@Entity 
public class College {
    @OneToMany(mappedBy="college")
    private Student student;
}

3. ManyToMany Mapping : ->

In many to many relationships, there are multiple instances related between two entities on both sides of relation. In this type of mapping, there is a separate table which maps the primary key of both the entities. An example of this type of relationship is between roles of a user and activities or privileges provided to that user. Many privileges may be mapped to a single role and many roles can have the same privilege. So, in this case, there is many to many mapping between Role entity and Privilege entity. 

Code for many to many relationships between Role and Privilege entity : ->

@Entity
public class Role {
     @ManyToMany
     private List Privilage privilage;
}
@Entity
public class Privilage {
     @ManyToMany(mappedBy="privilage")
     private List Role role;
}

So to conclude, these are the different types of associations between two entities in java. It is like the same way as in relational database or RDBMS.

About Author

Author Image
Mayank Madhav

Mayank is Associate Consultant Development - Java (Backend developer). Familiar with Spring Boot, Rest Api(Implementing Crud Operations) and building Micro services. Worked on Spring Security as well. Knowledge of ORM tool like Hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..