Hibernate FAQ: 1. What Is ORM? A 2. What Is Hibernate? A
Hibernate FAQ: 1. What Is ORM? A 2. What Is Hibernate? A
1. What is ORM?
A ORM stands for object/relational mapping. ORM is the automated persistence of objects in a
Java application to the tables in a relational database.
2. What is Hibernate?
A Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that
allows you to map plain old Java objects to relational database tables using (XML) configuration
files. Its purpose is to relieve the developer from a significant amount of relational data
persistence-related programming tasks.
3. What is the advantage of using hibernate?
A Following are the advantages of using Hibernate
a) Improved productivity:
High-level object-oriented API
Less Java code to write
No SQL to write
b) Improved performance:
Sophisticated caching
Lazy loading
Eager loading
c) Improved maintainability
A lot less code to write
d) Improved portability
ORM framework generates database-specific SQL for you
4. What are the Core interfaces are of Hibernate framework?
A The five core interfaces are used in just about every Hibernate application. Using these
interfaces, you can store and retrieve persistent objects and control transactions.
a) Session interface
b) SessionFactory interface
c) Configuration interface
d) Transaction interface
e) Query and Criteria interfaces
5. Difference between get() and load() method?
get() load()
returns null if object is not found in cache as It throws ObjectNotFoundException if object is
well as on database not found on cache as well as on database but
never return null.
get method always hit database load() method may not always hit the database,
depending upon which method is called.
get method never returns a proxy, it either while load() method may return proxy.
returns null or fully initialized Object.
save() saveOrUpdate()
It generates a new identifier and INSERT record saveOrUpdate can either INSERT or UPDATE
into database based upon existence of record.
save is faster compared to saveOrUpadate saveOrUpdate is slower because it needs to do
an extra search operation to find out whether
key is already there in database.
save return Serializable object It returns void
save() persist()
save return Serializable object persist returns void.
It guarantee that the identifier value will be persist() method doesn't guarantee that the
assigned to the persistent instance immediately identifier value will be assigned to the persistent
instance immediately, the assignment might
happen at flush time.
save() method does not guarantees that it will persist() method guarantees that it will not
not execute an INSERT statement if it is called execute an INSERT statement if it is called
outside of transaction boundaries outside of transaction boundaries
Step 1: Define the HQL query. This can be either done in xml mapping file or with annotations
a. With XML configuration:
<query name="Employee.findByEname">
select e.empid, e.ename,e.email from Employee e where
e.ename = :nme
</query>
Note: <query> tag should be declared after </class> tag
b. With annotations configuration: annotate the entity with @NamedQuery() as follows
@Entity
@Table(name="employee")
@NamedQuery(name="Employee.findByEname", query="select e.empid,
e.ename,e.email from Employee e where e.ename = :nme")
public class Employee {
Note: If you want to declare multiple HQL queries at once then you can use
@NamedQueries which accepts array of @NamedQuery
Step 2: To get the named query use the getNamedQuery() of Session interface. And use
setString() or setInteger() to give named parameter as follows
Query q = ses.getNamedQuery("Employee.findByEname").setString("nme",
"JAMES");
Q Difference between SQL and HQL
SQL HQL
SQL is based on a relational database model HQL is a combination of object-oriented
programming with relational database concepts
SQL manipulates data stored in tables and HQL is concerned about objects and its properties
modifies its rows and columns
SQL is concerned about the relationship that exists HQL considers the relation between two objects
between two tables
(Note: batch-size="" is also considered as a fetch strategy which Fetches up to ‘N’ collections
or entities)
Q Which SQL operations are performed when we call different methods of Session interface?