100% found this document useful (1 vote)
229 views7 pages

Hibernate Interview Questions and Answers

Hibernate interview questions and answers are provided regarding configuration files, mapping files, ORM tools, caching, object state transitions, generator classes, HQL, and Criteria API. Key points covered include: - Configuration files can set all properties while mapping files set locations; it is better to have one configuration file per database and one mapping file per module. - ORM tools convert object properties to database fields and save data without direct JDBC usage. - Objects transition between transient, persistent, and detached states through methods like save(), update(), and delete(). - Generator classes like identity, sequence, and increment set object IDs differently based on the database. - HQL is an object-oriented query

Uploaded by

Vinay Bayyaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
229 views7 pages

Hibernate Interview Questions and Answers

Hibernate interview questions and answers are provided regarding configuration files, mapping files, ORM tools, caching, object state transitions, generator classes, HQL, and Criteria API. Key points covered include: - Configuration files can set all properties while mapping files set locations; it is better to have one configuration file per database and one mapping file per module. - ORM tools convert object properties to database fields and save data without direct JDBC usage. - Objects transition between transient, persistent, and detached states through methods like save(), update(), and delete(). - Generator classes like identity, sequence, and increment set object IDs differently based on the database. - HQL is an object-oriented query

Uploaded by

Vinay Bayyaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Hibernate interview Questions and answers:-----------------------------------------------------Q. In how many ways you can set a configuration properties in hibernate?

Ans:- In 2 ways, By using configuration file and by using properties file.


Q. What is the difference between configuration file and properties file?
Ans:- By using propertiels file we can set connection properties and hibernate p
roperties but we cannot set location of mapping file but by using configuration
file we can set all of the above properties.
Q. How many hibernate mapping files required in a project?
Ans:- It is better for per one module one hibernate properties file.
Q. How many configuration file is need in a project?
Ans:- It is better for per one database one configuration file.
Q. What ORM tool does?
Ans:- It converts text properties into object internally and saves the data in d
atabase but by using JDBC it cannot be possible.
Q. Can we develope a hibernate application without properties file and configura
tion file ?
Ans:- YES, we can develope by hard coding. But there is a draw back because if a
ny changes done, then we nned to recompile the code, reload the application and
some times we have to restart the server.
Q. What are the hbm2ddl.auto properties?
Ans:- validate (default), create, update, create-update.
Q. How to active a caching in hibernate?
Ans:- As a developer, we need not to activate any cahing mechanism in hibernate.
Hibernate automatically active it when session is opened and automatically deac
tivate when session is closed.
Q. How an object is transfered to database by using hibernate?
Ans:- When we commit a transaction, automatically hibernate calls flush() method
internallya nd transfers an object to the database.
Q. What is the difference between directly updating an object and after loading
then updating an object in Hibernate?
Ans:- In Hibernate when we directly update an object if by mistake we forgot to
set any properties then hibernate set that properties as hibernate default value
(developer has to remember every property value before updating), but after loa
ding, developer not to remember all the properties values.
Q. How to convert an object form transient state to persistent state in Hibernat
e?
Ans:- By using methods like save(), update(), saveorupdate(), persist() etc.
Q. How to convert an object whose reference is null from transient state to pers
istent state in Hibernate?
Ans:- By using methods like load() and get().
Q. How to convert an object from persistent state to detached state in hibernate
?
Ans:- By using methods like clear() evict(), close() etc.
Q. How to convert an object from detached state to persistent state in hibernate
?

Ans:- By using methods like update(), saveorupdate(), merge() methods etc.


Q. How to convert an object from persistent state to transient state in hibernat
e?
Ans:- By using method like delete().
Q. What is the difference between update() and merge() method in hibernate?
Ans:- Both methods are used for converting an object from detached state to pers
istent state. While reattaching an object to a session, if that session cache al
ready contains an object with same idetifier then an exception (org.hibernate.No
nUniqueObjectException) will be thrown because a session cache can maintain only
one object with same id. (update method)
If a session cache doesn't have an object with same id then whenever update() me
thod is called, than a detached object will be attached to the session and there
will be no exception.
If a session canced already contains an object with same id, then instead of upd
ate() mehtod, wee call merge() method.
When merge() mehtod is called, then hibernate checks fo t the changes between de
tached object and an object in cache. If changes exists, then hibernate copies t
he changes from detached object to the object in the cahce.
-------------------------------------------------------------------------------------Generator class in hibernate:
-------------------------------------------------------------------------------------Q. What is the use of generator class in hibernate?
Ans:- To set an id for the object which is going to save in database.
Q. What is the default generator class of hibernate?
Ans:- Assigned
Q. How many types of generator class in hibernate and what are that classes?
Ans:- 7 types. Assigned, Increment, Sequence, hilo, identity, native and foreign
.
Q. What is the formula for calculation increment generator class?
Ans:- (max(id)+1)
Q. What is the formula for hilo generator?
Ans:- max_lo * next_hi + next_hi
Q. Mention database dependent and independent generator classes in hibernate?
Ans:- Independent ---> assigned, increment, foreign and native
Dependent ---> sequence, hilo, identity
Q. Discuss about generator classes?
Ans:Assigned:- It sets an id object according to programmer set the id for a
n object.
increment:- This generator class will select max no of existing id from
the table and adds by 1 for it.
Sequence:- This sequence generator will select a nextvalue of a dequence
form database and then returns that value to the hibernate.
Hibernate: select max(pid) from dual;
Hibernate: insert into products (pname, price, pid) valu
es (?,?,?)
<id name="productID" column="pid">
<generator class="sequence"> ---- sequence is the alia
s name
<param name="sequence">Test_sequence</param>

</generator>
</id>
hi/lo:- For the first time, hi/lo generator returns id as 1
From next time, hi/lo uses a formula max_lo * next_hi + next_hi.
Default max_lo value is : 32767 table: hibernate_unique_key, col
umn: next_hi
To change default setting, we uses a <param> tag
<id name="productID" column="pid">
<generator class="hilo">
<param name="max_lo">10</param>
<param name="table">Hilocount</param>
<param name="column">counts</param>
</generator>
</id>
identity:- For auto increment
native:- This generator works as sequence (or) identity (or) hilo
If sequence si not supported by the database then the native ge
nerator checks for identity. If identity is also not supported then finally it w
orks like hilo.
foreign:- This generator class is used to copy the id from parent object
to the id of child object in a one to one relationship.
Q. If a id column type of String then how can you define in generator class in h
ibernate?
Ans:- Hibernate uses a clss name uuid.hex (Universal unique id)
Algorithm: 1. IP address of system 2. startup time of jvm, 3. System time,
4. counter value of jvm
Q. What is custom generator in hibernate?
Ans:- If given hibernate class is not suitable for the project, developer can cr
eate own generator class in hibernate. This is called custom generator.
Q. How to generate custom generator in hibernate?
Ans:- To create a custom generator, we need to implemnt our class from "Idenfifi
erGenerator". It has one abstract method generate().
Q. What internally hibernate calls when you mention a generator class?
Ans:- If we mention a generator class as assigned, then hbernate internally call
s Assigned Generator so on.
Q.For Custom generator how can you define in id tag?
Ans:- <id name="productId" column="pid">
<generator class="MyIdGenerator"/> --- implenetation class of Identifier
Generator
</id>
-----------------------------------------------------------------------HQL (Hibernate Query Language)
-----------------------------------------------------------------------Q. What is HQL?
Ans:- It is an object oriented of SQL
Q. Why HQL is used in hibernate?
Ans:- To perform CURD operation in one or more object. (Both select and non sele
ct operation).
A select query of hibernate can retreive more than one partial object or f
ull object.
Q. What HQL does?
Ans:- HQL replaces properties names with column names and pojo class names with
table name.

Q. What is single row operation in hibernate?


Ans:- By using a method if you perform CURD operation on a single object, then t
hat is called single row operation.
Q. What is the name of the method used in select operation(HQL)?
Ans:- createQuery() of session interface, whic return type is Query interface: Q
uery query = session.createQuery("------");
J
ava.util.List list = query.list();
Q. What is the mehtod used in hql for an update operation?
Ans:- executeUpdate();
------------------------------------------------------------Criteria API (Only select operation)
------------------------------------------------------------Q. Why you need Criteria?
Ans:- It is used to perform bulk operation on database.
Q. How hibernate internally creates a query?
Ans:- Hibernate internally uses meta-data of a domain class.
Q. How to select data through Criteria?
Ans:-We need a reference of critertia. We can obtain a refernce by calling creat
eCriteria() method of session interface, For example,
Criteria criteria = session.createCriteria();
List list = criteria.list();
Q. How to add a condition to Criteria?
Ans:- We need an object of type org.hibernate.criterion.Criterion interface to r
epresent a condition.
We can obtain a criterion api object by calling static methods of org.hibe
rnate.Restriction class. Restriction is not implementation class of Criterion in
terface but both are from same
package. Restrictions is a Singleton class
.
Criteria crit = session.createCriteria(Employee.class);
Criterion c1 = Restrictions.eq("deptNumber", 20);
crit.add(c1);
List list = crit.list();
Q. How to add a sorting order to criteria?
Ans:- We need org.hibernate.criterion.Order
Criteria crit = session.createCriteria(Employee.class);
Order order = Order.desc("deptNumber");
crit.addOrder(order);
List list = crit.list();
Q. How to load a partial object in Criteria?
Ans:- 1) Create a Projection object for each property, 2) Add projection objecti
on to Projection list. 3) Set Projection list to Criteria
Criteria crit = session.creatCriteria(Employee.class);
Projection p1 = Projections.property("employeeId");
Projection p2 = Projections.property("employeeName");
ProjectionList projList = Projections.projectionList();
projList.add(p1);
projList.add(p2);
crit.setProjection(projList);
List list = crit.list();
In hibernate Projections are used, not only for reading partial object,
but also to find the result of aggregrate functions. For example,

-----------------------------------------------------------------------------------------------------------------------------------------------Create a Projection object for each property, 2) Add projection objectio


n to Projection list. 3) Set Projection list to Criteria
Criteria crit = session.creatCriteria(Employee.class);
Projection p1 = Projections.sum("employeeSalary");
Projection p2 = Projections.max("employeeSalary");
Projection p3 = Projections.min("employeeSalary");
ProjectionList projList = Projections.projectionList();
projList.add(p1);
projList.add(p2);
projList.add(p3);
crit.setProjection(projList);
List list = crit.list();
Q. How to get pagination technique in Criteria?
Ans:- To retreive particular range of objects like (sl no 20 to 30 objects) then
pagination technique used.
crit.setFirstResult(20); from sl no 20 onwards
crit.setMaxResult(10); next to 10 objects from sl no 20
Q. When do we choose Criteria in hibernate?
Ans:- If we want to retrive objects from database with multiple conditions then
we choose Criteria instead of HQL.
The reason why we select criteria in case of hql, we may do mistakes in
the query. But in Criteria query is internally constructed by hibernate.
Q. What is the difference between HQL and Criteria?
Ans:- Through HQL we can perform both select and non-select operations on object
but with criteria we can perform only select operaion.
Q. What is native SQL in hibernate?
Ans:- It is the way in which we can directly run SQL commands from a hibernate a
pplication. Specially it is used when we migrate a JDBC application into hiberna
te.
SQLQuery qry = session.createQuery("select * from emp").
List list = qry.list();
Q. Why addEntity() method is used in native SQL?
Ans:- If we want to tell the hibernate that, convert each row of ResultSet into
a domain class object internally then we call addEntity() mehtod.
Q. What is EntityQuery Object?
Ans:- If we want to tell the hibernate that, convert each row of ResultSet into
a domain class object internally then we call addEntity() mehtod. This is called
Entity Query object.
qry.addEntity(Employee.class);
Q. What is Scalar Query Object?
Ans:- While selecting partial object, internally hibernate will find the columns
stored in a ResultSet with the help of ResultSetMetadata.
We can explicitly tell the hivernate about the types of the column used
in the query by calling addScalar() method.
If it call addScalar() method then that query is called Scalar Query ob
ject.
query.addScalar("empno", Hibernate.INTEGER);
Q. What is Named Query in hibernate?
Ans:- If we want to run same query for multiple times for a multiple places then
we can make the query as re usable by using named query concept.

Q. How many types of Named Query in hibernate and what are they?
Ans: 2 types, 1. Named HQL Query 2. Named Native SQL query.
Q. How to configure Named Query in hibernate?
Ans:- After complete the <class> tag.
for named HQL query -------<class>
</class>
<query name="q1" >from Employee e</query> ---- Here Employee is the doma
in class name
for named SQL Query
<class>
</class>
<sql-query name="q1" >select * from emp</sql-query> --- Here emp is the
database table name
Q. What is inheritance mapping in hibernate and in how can we acheive it?
Ans:- When inheritance exists in doamin class in java, to configure the properti
es of inheritance domain class and database, we use inheritance mapping in hiber
nate.
By using three ways we can acheive it:Table per class:
If one table is given at database for all class
of hireachy then we apply table per class.
Table per concrete-class:
When we want to map each sub class of hi
reacky to separate table of database.
Table per sub-class:
Whenever we want to map each class of hierachy t
o a separate table of database.
Q. How to configure inheritance strategies in hibernate?
Ans:- table per class: In this case, database shoud maintain one extra column
to identify the class. That column is called discriminator column.
In hbm file, we write after the id column,
<hibernate-mapping>
<class name="fully qualified package name" table="table
name">
<id></id>
<discriminator column="pmode" type="string" lenth="5"/>
<sub class name="fully qualified with package name" disc
riminator-value="any name">
---------</sub class>
</class>
</hibernate-mapping>
table per concrete class: In this strategy, we don't need discriminator
column, in place of <sub class> we write <union-sublcass>
<hibernate-mapping>
<class name="fully qualified class name">
<id></id>
<union-subclass name="fully qualified with package name"
table="any name">
---------</union-subclass>
</class>
</hibernate-mapping>
table per sub class:
<hibernate-mapping>
<class name="fully qualified class name" table ="table n

ame">
<id></id>
<joined-subclass name="fully qualified with package name
" table="any name">
---------</joined-subclass>
</class>
</hibernate-mapping>
Q. What is versioning of an object in hibernate and how can we configure it?
Ans:- Versioning is a concept in which hbernate will maintain the no of times
of an object is updated in database. Hibernate automatically increment version o
f an bject and stores it in a database table whenever an object is updated.
We need to add a property type int in domain class. We need to configre the <ve
rsion> tag immediate after to the <id> tag
private int version; // setter and getter mehtod in domain class.
<id></id>
<version name="domain class property name" column="any name"/>
Q. What is Time Stamp feature in hibernate?
Ans:-

You might also like