Hibernate Interview Questions – Part 3

Hibernate Interview Questions – Part 3

Hibernate Interview Questions – Part 1Part 2

Explain One – to – One Mapping:
Example:
Consider we have 2 tables, Employee and Employee Address. We save the employee details at one table like his name, employee number etc and we save employee address in another table. We will have a one to one relationship for specific employee from employee table to specific records in Employee Address table.

Consider there is an employee named Alpha in Employee table and let the primary key for Alpha be 1. Now the table Employee will have an column that links to Employee Address table and that Employee Address will point to a record in Employee Address table.

Detailed Explanation Here


Explain OneToMany Mapping:
Example:
Consider an employee might have more than one address, So as OnetoMany mapping points out, A single employee can have more than one address in another table. (Detail Here)


Explain ManytoMany Mapping:
let us consider 2 entities – Employee and Address. An employee can have more than one address let us assume permanent and temporary address, Same way more than one employee would have stayed in the temporary address. This scenario shows Employee can have more than one address and an address can have more than one employee. This is ManyToMany mapping. (Details Here)


Explain Table per Concrete Class:
In this method, a table is created for each class in the inheritance. Each table will have its own property columns and also has its base class properties. We will have repeated columns of base class in all sub classes


Explain Table per Class:
In this method, a single table is created for all classes both Base and Subclass. We have to define a column Discriminator column to differentiate object of base and subclass.(Detailed Explanation Here)


Explain Table per Subclass:
This method, creates a table for each subclass. The difference between table per concrete class and Table per subclass is the Inherited tables will not create columns of base class. It uses foreign key and points to base class for base class properties(Detailed Explanation Here)


 

Read More

ManyToMany Mapping in Hibernate using Annotations

ManyToMany Mapping in Hibernate using Annotations
In our previous examples we have seen OneToOne and OneToMany mappings. In this article let us see how to implement ManyToMany annotation in Hibernate using Annotations.

Let us understand how ManyToMany mapping works. For example, let us consider 2 entities – Employee and Address. An employee can have more than one address let us assume permanent and temporary address, Same way more than one employee would have stayed in the temporary address. This scenario shows Employee can have more than one address and an address can have more than one employee. This is ManyToMany mapping.

screen1

This is a simple example, From the diagram we can understand that an employee 1 has more than one address associated with him, same way temporary address has more than one employee associated with it. This is known as ManyToMany mapping.

Now let us see an example.

Structure
Structure

hibernate.cfg.xml

NewHibernateUtil.java

Address.java

Employee.java

Execution.java

Output:
op1

From the output,
Employee_Mapping table saves both the employee records

op4

Address_Mapping table saves both the addresses

op5

Employee_Mapping_Address_Mapping has the employee ID mapped to the corresponding address
op2
we can see that employee with id: 1 is mapped with address ID 1 and 2

 Address_Mapping_Employee_Mapping
Same way here the address 1 is mapped to both Employee’s with ID 1 and 2
op3

 

 

 

Read More