What is Hibernate ?
Hibernate is an ORM tool that maps POJOs to tables in relational database.
The mapping is done through a xml configuration file or through annotations.
What is ORM framework ?
Object Relational Mapping frameworks like Hibernate, IBatis etc provide mapping between code and tables.
This helps productivity as the developers can focus on their business logic rather than coding for database connectivity.
It provides improved performance through features like caching, lazy loading, eager loading etc.
The framework helps removing application’s dependency on particular database. The application code remains portable across databases.
What is Session in Hibernate ?
Session represents a conversation between the application and database.
It is single threaded.
It holds a mandatory first level cache of persistent objects.
Session object is obtained from SessionFactory.
What is SessionFactory in Hibernate ?
Session is obtained from SessionFactory.
Typically there is only one Session Factory for the whole application.
What are different types of caches in Hibernate ?
Hibernate uses two different type of caches for objects: first-level cache and second-level cache.
First level of cache is associated with Session object, while second-level of cache is associated with the SessionFactory object.
By default, Hibernate uses first-level of cache on a per-transaction basis. Hibernate mainly use this cache to reduce the number of SQL queries it needs to generate within a given transaction.
What is the difference between get() and load() methods in Hibernate ?
– Use load() method only when you are sure that object you want to read already exists.
– If unique Id of an object does not exists in database then load() method will throw an exception.
– load() method return proxy object default and database won’t be hit until the proxy is first invoked.
– Use get() method when you are not sure about the object existence in the database.
– If object does not exists in the database, the get() method will return null.
– get() method will hit database immediately.
When to use merge and when to use update method in Hibernate ?
Use update() method when you are sure that session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.
What is Hibernate Query Language (HQL) ? Explain how to perform DML operations using HQL.
HQL provides an easier way to query the tables.
It uses Class name in place of Table name and Property name in place of column name
See below for syntax of Select, Update and Insert operations.
Query query = session.createQuery(“from Employee where empid = :empid”);
List empList = query.list();
Query query = session.createQuery(“from Employee where empid = ?”);
query.setParameter(0, “123”); //can also use setString() or setProperties(<object>)
List empList = query.list();
Query query = session.createQuery(“Update Employee set empname = “:empname” where empid=:empid”); query.setParameter(empname, “abc”); query.setParameter(empid, “123”); int result = query.executeUpdate();
insert into tablename values(…) is not supported. Inserting into a table by selecting values from another table is supported.
Query query = session.createQuery(“Insert into employee(empid, empname) select empid,empname from back_emp”);
int result = query.executeUpdate();
What is Criteria api ? What are the advantages and disadvantages of it ?
Criteria api provides an Object Oriented and elegant approach compared to HQL.
Here an example of how to use criteria to select data from a table.
Criteria criteria = session.createCriteria(Employee.class); criteria.addOrder(Order.asc(“emp_id”)); criteria.add(Restrictions.eq(“emp_id”, “123”)); criteria.list();
Issue with this:
– Query scattered across code.
– Can’t control the query generated by hibernate.
How to do pagination in Hibernate using Criteria api ?
For pagination, maxresults and first result need to be set as follows :
What are Named Queries ? Explain how to use them .
Named queries are easier to maintain as they are added in the mapping xml files.
Query names should be unique.
Here is an example of a named query:
<query name=”query1”> <![CDATA[ from employee where emp_id = :emp_id ]]> </query>
CDATA blocks avoids warning because of > or < symbol.
Named query should be added only after the Class element in the mapping file.
Here is how the Named query can be invoked in code:
Query query = session.getNamedQuery(“query1”); query.setString(emp_id, “123”); query.list();
What are Native Queries in Hibernate ?
Native Queries can be added similar to Named Queries. However, the query added is database specific.
Normally, native queries are used when a query is already optimized by database developers and dbas and should be used as is in the application rather than depending on Hibernate to generate and optimize it.
Here is an example:
<sql-query name=”query1”> <![CDATA[ select * from employee where emp_id = :emp_id ]] </sql_query>
© 2015 – 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post