Hibernate Interview Questions and Answers
Hibernate Interview Questions and Answers
</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. 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:-