Hibernate
interview questions
Here I have capture important interview
questions on Hibernate.
1. What are the
core interfaces of Hibernate?
The core interfaces of Hibernate framework are:
1)
Configuration
2)
SessionFactory
3)
Session
4)
Query
5)
Criteria
6)
Transaction
2. Is
SessionFactory a thread-safe object?
Yes, SessionFactory is a thread-safe object, many threads cannot access
it simultaneously.
3. What the
difference is between get and load method?
The differences between get() and load() methods are given below.
get()
|
load()
|
Returns null if object is
not found.
|
Throws
ObjectNotFoundException if object is not found.
|
get() method always hit
the database.
|
load() method doesn't hit
the database.
|
It returns real object
not proxy.
|
It returns proxy object.
|
It should be used if you
are not sure about the existence of instance.
|
It should be used if you
are sure that instance exists.
|
4. How many types
of association mapping are possible in hibernate?
1)
One to One
2)
One to Many
3)
Many to One
4)
Many to Many
5. Explain One to
One mapping?
One to one mapping maintain one to one relation between two records.
Suppose there is an Employee record and each employee have a unique address
which means there is one to one relation with employee and address
public class Employee{
private int id;
private String empName;
private Address address;
….
}
public class Address{
private int id;
private String addressDetails;
private String zipcode;
….
}
Employee
table will maintain a foreign key for Address table.
One to one
xml mapping
<one-to-one name="address" cascade="all"></one-to-one>
6. Explain One to
Many mapping?
In above example suppose one employee have multiple address that might be
permanent address, temporary address, official address etc.
In address table each address line will contain the employee id to
maintain the one to many relation as name suggest employee table have one
record and for that record there will be many record in address table.
public class Employee{
private int id;
private List<Address> addresses;
….
}
Employee
table contain list of address
One to Many
xml mapping
<list name="addresses"
cascade="all">
<key
column="id"/>
<one-to-many class="Address"/>
</list>
7. Explain Many
to One mapping?
In above example suppose many employees are associated with the same
address.
Employee object will contain an address object
Many to One
xml mapping
<many-to-one name="address" column="address"
class="Address"
not-null="true"/>
8. Explain Many
to many mapping?
A Many-to-Many mapping can be implemented using a Set java collection
that does not contain any duplicate element.
Suppose many employee belongs to multiple address and multiple address
belongs to multiple employees.
Many to Many
xml mapping
<set name="addresses" table="emp_addrs">
<key column="emp_id"/>
<many-to-many
column="addrs_id" class="Address"/>
</set>
9. Explain inheritance
mapping?
10. What is lazy
loading?
Lazy loading is a technique in which objects are loaded on demand basis,
by default child objects are not loaded when parent is loaded
11. What is first
level cache in hibernate?
The first-level cache is the Session cache
12. What is second
level cache in hibernate?
Second-level cache can be configured on a per-class and per-collection
basis and mainly responsible for caching objects across sessions.
Nice information, very usefull thanks
ReplyDelete