Hibernate Interview Question
- What is Hibernate?
It is an open source and lightweight ORM tool that is used to store, retrieve and manipulate data from a database. It is one of the most widely used ORM tools for Java applications.
- What is HQL?
Hibernate Query Language (HQL) is the same as Structure Query Language. It is known as an object-oriented query language. Here we use an object to store our database and it is independent of the database.
- What is ORM?
Object Relation Mapping (ORM). It is a programming strategy to map object with the data stored in the database. It simplifies data creation, data manipulation, and data access.
- Explain the advantages of Hibernate?
Some of the advantages of Hibernate are:
- It provides Simple Querying of data.
- An application server is not required to operate.
- The complex associations of objects in the database can be manipulated.
- Database access is minimized with smart fetching strategies.
- It manages the mapping of Java classes to database tables without writing any code.
- Properties of an XML file is changed in case of any required change in the database.
- What are the Key components/core interfaces of Hibernate?
The Key components/core interfaces of Hibernate framework are:
- Session: It is used to get a physical network with a database.
- Transaction: It represents the unit of work with a database.
- Query: It uses SQL and HQL string to retrieve the data from the database and create objects.
- Criteria: It is used to create and execute object-oriented queries and retrieve the objects.
- Configuration: It represents the properties of files required by Hibernate
- Session Factory: It configures hibernate for the application using the provided configuration file and instantiates the session object.
- What are the advantages of using ORM over JDBC?
ORM has the following advantages over JDBC:
- Application development is fast.
- Management of transaction.
- Generates key automatically.
- Details of SQL queries are hidden.
- Define criteria in terms of Hibernate.
Criteria objects are used to create and execute object-oriented Queries to retrieve the objects.
- Mention some of the databases that Hibernate supports?
Hibernate supports all the major RDMS. Following are the list of database engines supported by Hibernate:
- HSQL Database Engine
- Microsoft SQL Server Database
- Sybase SQL Server
- Informix Dynamic Server
- Mention two components of Hibernate configuration object.
- Database Connection
- Class Mapping Setup
- How is SQL query created in Hibernate?
Syntax of creating SQL query: Session.createSQLQuery
- How is the HQL query created?
Syntax of creating HQL query: Session.createQuery
- Define persistent classes.
Those classes objects are stored in a database table are called a persistent class.
- What is SessionFactory?
SessionFactory provides the instance of Session. It is a factory of Session. It holds the data of second level cache that is not enabled by default.
- Is SessionFactory a thread-safe object?
Yes, many threads cannot access it simultaneously.
- What is Session?
It maintains a connection between the hibernate application and database.
It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.
It is a factory of Query, Criteria and Transaction i.e. it provides factory methods to return these instances.
- Is Session a thread-safe object?
No, Session is not a thread-safe object; many threads can access it simultaneously. We can share it between threads.
- What are the states of the object in hibernate?
There are 3 states of the object (instance) in hibernate.
- Transient: The object is in a transient state if it is just created but has no primary key (identifier) and not associated with a session.
- Persistent: The object is in a persistent state if a session is open, and you just saved the instance in the database or retrieved the instance from the database.
- Detached: The object is in a detached state if a session is closed. After detached state, the object comes to persistent state if you call lock() or update() method.
18. How to make an immutable class in hibernate?
By marking a class as mutable=”false”, it will be treated as an immutable class. By default, it is mutable=”true”.
- What is automatic dirty checking in hibernate?
The automatic dirty checking feature of Hibernate, calls update statement automatically on the objects that are modified in a transaction.
Here, after getting employee instance e1 and we are changing the state of e1.
After changing the state, we are committing the transaction. In such a case, the state will be updated automatically. This is known as dirty checking in hibernate.
- How many types of association mapping are possible in hibernate?
There can be 4 types of association mapping in hibernate.
- One to One
- One to Many
- Many to One
- Many to Many
- Is it possible to perform collection mapping with One-to-One and Many-to-One?
No, collection mapping can only be performed with One-to-Many and Many-to-Many.
- What inheritance mapping strategies are available in Hibernate?
Hibernate have 3 ways of inheritance mapping, they are
- Table per hierarchy
- Table per concrete class
- Table per subclass
23. What is lazy loading in hibernate?
Lazy loading in hibernate improves the performance. It loads the child objects on demand.
Since Hibernate 3, lazy loading is enabled by default, and we don't need to do lazy="true". It means not to load the child objects when the parent is loaded.
- What is the difference between first level cache and second level cache?
First Level Cache
|Second Level Cache|
- What is the difference between update and merge method?
|The update() method||merge() method|
26. What is the difference between get and load method?
- What is Hibernate caching?
Hibernate caches Query data and makes the application run faster.
If used correctly, the hibernate cache can be very useful in achieving the faster application running performance.
The main idea lying behind the cache is reducing the number of database queries, which results in reduced throughput time of the application.
28. Explain the persistent classes in Hibernate?
In hibernate, the Java classes whose instances and objects are stored in database classes are called persistent classes.
- Which annotation is used to declare a class as a hibernate bean?
@Entity annotation is used to declare a class as an entity.
- How to write composite key mapping for hibernate?
If the table has a primary key then in the hibernate mapping file we need to configure that column by using <id /> element right..!
in order to configure these primary key columns in the hibernate mapping file we need to use one new element called <composite-id …..> </composite-id>
- What is the difference between save and persist in hibernate?
Save return - Returns Serializable object
Persist return - Returns Void
- What are the important interfaces of Hibernate framework?
Important interfaces of Hibernate framework are:
- SessionFactory (org.hibernate.SessionFactory)
- Session (org.hibernate.Session)
- Transaction (org.hibernate.transaction)
- What is the difference between Lazy and Eager loading in hibernate?
Eager Loading is a design pattern in which data initialization occurs on the spot. it fetches the child entities along with parent.
Lazy Loading is a design pattern which is used to defer initialization of an object as long as it’s possible. It fetches the child entities lazily, that is, at the time of fetching parent entity it just fetches proxy of the child entities and when you access any property of child entity then it is actually fetched by hibernate.