Hibernate Interview Questions answer 2016

Hibernate Interview Questions answer 2016.

1. What are the types of inheritence models and describe how they work like vertical inheritence
and horizontal ?
Ans. (a)
There are three types of inheritance mapping in hibernate
1. Table per concrete class with unions
2. Table per class hierarchy
3. Table per subclass
Let us take the simple example of 3 java classes.
Class Manager and Worker are inherited from Employee Abstract class.
1. Table per concrete class with unions
In this case there will be 2 tables
Tables: Manager, Worker [all common attributes will be duplicated]
2. Table per class hierarchy
Single Table can be mapped to a class hierarchy
There will be only one table in database called ‘Employee’ that will represent all the attributes required for all
3 classes.
But it needs some discriminating column to differentiate between Manager and worker;
3. Table per subclass
In this case there will be 3 tables represent Employee, Manager and Worker
Ans. (b). Table per concrete class using implicit
polymorphism can also be added as inheritance mapping in hibernate
2. what is lazy fetching in hibernate ?
Ans 1
Lazy setting decides whether to load child objects while loading the Parent Object.You need to do this setting
respective hibernate mapping file of the parent class.Lazy = true (means not to load child)By default the lazy
loading of the child objects is true. This make sure that the child objects are not loaded unless they are
explicitly invoked in the application by calling getChild() method on parent.In this case hibernate issues a
fresh database call to load the child when getChild() is actully called on the Parent object.But in some cases
you do need to load the child objects when parent is loaded. Just make the lazy=false and hibernate will load
the child when parent is loaded from the database.Exampleslazy=true (default)Address child of User class
can be made lazy if it is not required frequently.lazy=falseBut you may need to load the Author object for
Book parent whenever you deal with the book for online bookshop.
Ans 2
Lazy fetching is related to loading of the Child objects for its parent ( In the terms of databse its primary keyforeign
key relationship). In hbm.xml file you have to specify whether you want to load the child objects
while loading the parent. By default hibernate doesn’t load the whole child objects (lazy=”true”).
But Sometimes it doesn’t work properly, it doesn’t load the child objects for the parent.and second
problem is that if you have condition like ….
Employee Table -1—–n-> Emp_Dept Table<-n——1- Department Table
And you have many to many relationship between them, then by specifying lazy=”false” for both
the parent object(Employee And Department) hibernate try to get all the child for this parent and for this
child. Now this child becomes the parent and hibernate will try to get all the child for this parent. This
process may continue.This results in performance issue.
So U have to take care while writing the mapping files in case of many-to-many relationships.
Ans 3
Simply saying: It maintains the relactionship between two tables.
Ans 4.
There are two types of Loading in application. Eager loading and Lazy
loading. In eager loading we will fetch all the values from the Persistent storage
and cache it. It will make serious performance issues. There we use lazy
loading to avoid that scenario.
Some times we don’t need to load the child
table values, In that case we have to us lazy = true in .hbm file. so hibernate
will fetch only parent table values. Internally it will load the wrapper class
but it does not cache all the values till we perform operation.
Advantage: It will avoid caching unnecessary values and improve performances.

.pdf FileDownload Click here

Need More? Click Here