Interview_questions_on_hibernate
Interview_questions_on_hibernate
ORM stands for Object Relational Mapping. It is a way to save or mapped object in
the database. It simplifies creation of data, updation and deletion of data. There are
varieties of ORM tools available in the market like ibatis,toplink, hibernate etc
Configuration
SessionFactory
Factory object holds information about Jdbc connection. And this factory
object will be created using the interface called “SessionFactory”. We need to
create a factory object one per database using a separate configuration file. if
you are using multiple databases in our app, then you will have to create
multiple SessionFactory objects. SessionFactory object is heavy weight
Session
Query
Query is an inbuilt interface used to represent a hibernate query in an object
oriented format. As query is an interface, the object of it can be obtained using
session.createQuery() method of session interface. It is used to execute HQL
queries in the database
Criteria
The Hibernate Criteria Query Language is used to fetch the records of a table based
on some criteria or constraints. Criteria API is nothing but set of methods that allows
programmer to fire complex type of query onto the database by applying different
kinds of filtration rules and logical conditions. We can fetch complex type data from
the database without applying queries
Transaction
A transaction is associated with a session object and usually created by calling
session.beginTransaction() method. Basically, insert,update,and delete operations
make changes into the database, which means this operation performs transactions
into the database. Each transaction needs to be committed before executing that
transactions into the database
get() load()
get() will fire select query Load method does not fire a query everytime and can
everytime whenever we return a proxy in place and only initailize the object or
are using that object or fire the query , if any other getter method other than
not getId() is called on entity object
Use if u are not sure that Use if you are sure that object exists
object exists in db or not
HQL(methods) SQL(query)
1.One to One
In case of One to one mapping, one record of one table should exactly mapped with
one record of another table and In such case, a foreign key is created in the primary
table. For an example, One question is having only one answer and one person is
having only one adhar number
3.Many to One
In simple terms, many to one mapping means that many row in a table can be
mapped to exactly one row in another table
For an example, Multiple answers can be associated with only one question and
multiple bank accounts associated with one person
4.Many to many
In simple terms, one to many mapping means that many rows in a table can be
mapped to multiple rows in another table.
Example, Multiple employees can be worked on multiple projects and that each
project is done by multiple employees.
second level cache does not fire select query in multiple sessions for all sessions in
a one factory,it will fire select query only once
By default first level cache is enabled in hibernate. To enable Second Level Caching
for Hibernate , add this property to your xml file:
<property name="cache.use_second_level_cache">true</property>
<property
name="cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheR
egionFactory</property>
@Cacheable //means that the entity and its state/properties must be cached in the
hibernate