May 05, 2017

#Part 3: Hibernate Interview Questions (Mapping Relations)

How to achieve mapping in Hibernate?
Association mappings are one of the key features of Hibernate. It supports the same associations as the relational database model. We can map each of the below associations as a uni or bidirectional association.
  • One-to-One associations
  • Many-to-One associations
  • Many-to-Many associations

What is One-to-One association in Hibernate?
In this type of mapping, we only need to model the system for the entity for which we want to navigate the relationship in our query or domain model. We need an entity attribute that represents the association, so annotate it with an @OneToOne annotation.

Let's understand this taking an example:

We will create Person and Car class, we will assume a person can own only one car and one car can be owned by only one person.


When we run oneToOne() it will create two tables 'Person' and 'Car'. Person table will have a foreign key of Car. If you look into the log, you will find below queries.

Insert into Car (cid, carName) values (?, ?);
Insert into Person (pid, personName, Car_cid) values(?, ?, ?);

What is One-to-Many association in Hibernate?
In this type of association, one object can be associated with multiple/different objects. Talking about the mapping, the One-to-Many mapping is implemented using a Set Java collection that does not have any redundant element. This One-to-Many element of the set indicates the relation of one object to multiple objects.

Let's understand this taking an example:

We will create Person and Car class, we will assume a person can own many car's and one car can be owned by only one person.

When we run oneToMany() it will create two tables 'Person' and 'Car'. Person table will have a foreign key of Car. If you look into the log, you will find below queries.

Insert into Car (cid, carName) values (?, ?);
Insert into Person (pid, personName, Car_cid) values(?, ?, ?);

If we do not add mappedBy in the Person class, and run oneToMany() it will create three tables 'Person', 'Car' and 'Car_Person'.

@OneToMany (mappedBy="person")
private List cars=new ArrayList();

The value of mappedBy should be same as the variable in Car class. When we do not mention mappedBy another extra table with the mappings will be created.

Insert into Car (cid, carName) values (?, ?);
Insert into Person (pid, personName) values(?, ?);
Insert into Car_Person (Car_cid, Person_pid) values (?, ?);

What is Many-to-Many association in Hibernate?
Many-to-Many mapping requires an entity attribute and a @ManyToMany annotation. It can either be unidirectional and bidirectional. In Unidirectional, the attributes model the association and you can use it to navigate it in your domain model or JPQL queries. The annotation tells Hibernate to map a Many-to-Many association. The bidirectional relationship, mapping allows you to navigate the association in both directions.

Let's understand this taking an example:

We will create Person and Car class, we will assume a person can own many car's and one car can be owned by many person.


When we run manyToMany() it will create three tables 'Person', 'Car' and Person_Car.

Insert into Car (cid, carName) values (?, ?);
Insert into Person (pid, personName) values(?, ?);
Insert into Car_Person (Car_cid, Person_pid) values (?, ?);

If we do not add mappedBy in the Person class, and run manyToMany() it will create four tables 'Person', 'Car', 'Person_Car' and 'Car_Person'.

@ManyToMany (mappedBy="person")
private List cars=new ArrayList();

The value of mappedBy should be same as the variable in Car class. When we do not mention mappedBy another extra table with the mappings will be created.

Insert into Car (cid, carName) values (?, ?);
Insert into Person (pid, personName) values(?, ?);
Insert into Person_Car (Car_cid, Person_pid) values (?, ?);
Insert into Car_Person (Car_cid, Person_pid) values (?, ?);


-K Himaanshu Shuklaa..

No comments:

Post a Comment