23rd Dec, 2019
Hibernate is referred to as an open-source relational mapping also known as the ORM tool that allocates a structure to map object-oriented domain models to analogous databases for web applications. Object-oriented relational mapping (ORM) is emanated on carrying and stocking of various objects and abstraction that provides the extent. It is the abstraction that makes the addressing, accessing and manipulating objects easier by not bothering about their related data sources.
Below are few major features of Hibernate
Q1. What is Hibernate?
Hibernate is an open-source Java framework for mapping java objects with a database. It is an Object Relational Mapping tool that implements the Java Persistence API for creating persistence with the database. It is a lightweight and high-performance framework that is used to map Java classes and types to database tables and SQL data types.
It also gives the developer the tool to perform data queries and retrieving options with the database. The Hibernate open-source framework is licensed under the GNU LGPL (Lesser General Public License).
Q2. Enlist major features of Hibernate?
Some of the Major Features in Hibernate Are:-
Q3. Enlist core components of Hibernate?
The core components in the Hibernate architecture are,
Configuration Object – It has the configuration properties of the Hibernate. It is created first when creating the hibernate application. It has two components, Database connection, and class mapping setup. The Database connection provides the configuration for the connection. The class mapping creates the connection between the java class and the database.
SessionFactory Object – It is used to configure the Hibernate application. It is instantiated by the Configuration Object for every database using a separate configuration file.
Session Object – It is used to create a physical connection with the database. It is instantiated every time by the SessionFactory object to interact with the database.
Transaction Object – It is used to handle the transaction works in the Hibernate framework.
Query Object – It is used to create objects or to retrieve data from the database using HQL or SQL.
Criteria Object – It is used to retrieve objects by executing the object-oriented criteria queries.
Q4. What is n+1 select problem in Hibernate
The N+1 Selects is a problem related to the performance issue the Hibernate. In this problem, multiple queries are fired for a single query. Let's say that you have a collection of phone vendors and each vendor has a collection of phone models. To print all the phone models from each vendor, you’d have a query like the following,
Select * from Vendor; Select * from models where Vendor = ?;
Here we have one select query for fetching all vendors and another for selecting all the phone models from each vendor. We do the N+1 select query. It is an example of an N+1 problem in Hibernate. To avoid this, hibernate has an option that traces all the SQL queries to find the N+1 problem. Then, you can resolve this problem by firing a query that joins the two tables (vendor and phone models) and gets the result in a single query.
Q5. What is session in Hibernate?
A Session is an object in Hibernate that is used to create a physical connection with the database. This object is created each time when the database needs to be connected and interacted.
After the connection with the database, the session object is destroyed as it is not a thread-safe. It provides create, read, delete operations. The session object may be in a transient state, persistent state, or detached state.
Q6. Explain caching in Hibernate?
Caching is used to improve the performance of the hibernate framework by storing the recently used data in the cache memory.
This reduces the number of fetches to the database to get the element. Hibernate has three levels of caches,
First level cache – It is the session cache that is used for passing requests to the database. This cache is enabled by default but is not accessible to the entire application.
Second Level Cache – It is an optional cache that needs to be enabled to use. This level is accessible to the entire application. This is used to cache session objects.
Query Level Cache – This is also an optional cache that is used to store queries that are executed frequently with the same parameters. This is integrated closely with the second-level cache.
Q7. What is Hibernate SessionFactory?
Hibernate SessionFactory is an object created by the configuration object. It is a heavyweight object that contains all the configuration properties related to the database. A single SessionFactory object is created for every database in the application.
It is created when the application starts and is kept for later use. It is used by all the threads in the application as it is a thread-safe object.
Q8. What is difference between load() and get() in Hibernate?
The get and load method are both derived from the Hibernate Session class and are used to retrieve objects from the database.
But there is a difference in how they are retrieving the objects.
Session get method - This method returns the real object when it is not present in the session cache by fetching it from the database. If the object is not found, then the get method returns null. It may involve multiple round trips to the database that may not be effective in all the cases and also reduces the performance.
Session load method – This method returns a proxy when the real object is not in cache. It initialized the object only if a method other than getid() is called on persistent. It doesn’t always hit the database and returns a proxy. So, it offers better performance than the session get method.
Q9. Enlist different states of object in Hibernate?
In hibernate, Objects are represented in any one of the following three states,
Transient Object State – In this state, the object is not linked with any hibernate session and does not represent in any row of the database. An object is in the transient state if it is created for the first time with the new() operator. It doesn’t contain any primary key value and it will be collected into the garbage if no other object refers to it.
Persistent Object State – In this state, an object will be associated with the hibernate session. It represents a row in the database. It also has a primary key value. This object can be created by associating a transient object with the session.
Detached Object State – These are the objects that are removed from the hibernate session. These objects contain identity and changes made to these objects are not saved in the database. Also, persistence operation with the detached object is not possible.
Q10. What is Isolation and Propagation in Hibernate?
The Propagation and Isolation are parameters in the @Transactional annotation.
Propagation – It defines how transactions are related to each other. It has the following options,
Isolation – It defines the data contract between the transactions. It has options such as Read Uncommitted, Read Committed, Repeatable Read, and Serializable. Uncommitted allows dirty read. Read Uncommitted does not allow a dirty read. Serializable performs all actions in sequence. Repeatable Read results in the same if a row is read twice in the transaction.
Q11. What is HQL?
HQL (Hibernate Query Language) is an object-oriented querying language used in the Hibernate framework.
Q12. What is criterion query in hibernate?
The Hibernate Criteria Query is one of the ways to retrieve data from the database in the hibernate framework. The criteria API is used to created complex nested queries in Java for executing database related operations. It also includes compile-time syntax and QBE (Query By Example) functionality. With QBE, you can pass objects with properties to retrieve data from the database. It also has support for projection and aggregation methods.
Q13. What are annotations in Hibernate?
Annotations can be used in the hibernate framework to define the mapping. It can be used standalone or with XML to create the mapping. The hibernate annotations are based on the JPA 2 specification and are defined in javax.persistence package. It provides a way to create the metadata for the relational table and object mapping. It is used to represent mapping information but it is not as flexible as the XML mapping.
Q14. Explain Hibernate object lifecycle?
In hibernate, an object will belong in any of the following three states,
Q15. What are different types of mappings available in Hibernate?
The Hibernate Mapping types are declared and used in the mapping files. These types get converted from Java type to SQL type. There are many mapping types in Hibernate like primitive types, data and time types, binary types, and JDK related types.
Primitive types include integer, long, short, float, double, string, byte, Boolean, character, and big_decimal.
Date and time types include data, time, calendar, timestamp, and calendar_date.
Binary and Object types include binary, text, clob, blob, and serializable.
JDK-related types include class, locale, timezone, and currency.
Q16. How to perform Select, Update, and Delete Query in Hibernate?
HQL is similar to SQL but it is a database-independent query language.
For the Select operation in HQL, it is like reading an entity. To select all the columns in the database, use from Employee e;
//here we select all columns from the employee database.
To select a specific column using HQL,
Select e.employeeID from Employee e; //here we select only employee ID from the database.
For update operation HQL, use the ‘update’ statement.
update Employee e set e.salary = 3500 where e.employeeID = 1; // here we update the salary for an employee using an ID.
For delete operation in HQL, use the ‘delete’ statement.
delete from Employee e where e.employeeID = 1; //here we delete the employee record based on ID.
Q17. What are Generators in Hibernate?
The Generator class in the hibernate framework is used to create a unique ID for the persistent objects.
The hibernate framework has many generator classes. They are,
Q18. Explain Projections in Hibernate?
Projection in Hibernate is a class for creating the projection objects. It is defined in “org.hibernate.criterion” package. It is used to query a subset of attributes of a group of entities.
It is also used to distinct clauses or aggregate functions like max, and sum.
Q19. Explain Named Query in Hibernate with example?
The Named Query in Hibernate is used to group HQL queries by some name like an alias. Then, we can reference the group of queries using the alias name. Hibernate named queries can be created by using either annotation or mapping file. It helps remove the scatter in the HQL query and cleans up the code.
By using a Named Query, you also have the advantage of reusing the code and making it fail fast in case of error.
Q20. What is difference between save() and saveOrUpdate() method in Hibernate?
The save() and saveorupdate() method is used to move the object form transient state to persistent state but there is a significant difference between them.
The save() method creates a new identifier for the object and inserts it into the database.
The saveorupdate() method does either insert or update operation based upon the existence of the object. Also, save() method brings only the transient state object to persistent state whereas saveorupdate() methods bring both the transient and detached state object into the persistent state.