Skip to content

Commit 4110f56

Browse files
committed
Update hibernate-questions.md
1 parent fd2f1b7 commit 4110f56

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

hibernate-questions.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## Hibernate Interview Questions & Answers
1+
# Hibernate Interview Questions & Answers
22

3-
#### Q. How to integrate hibernate with spring boot?
3+
## Q. How to integrate hibernate with spring boot?
44

55
**Step 01: Maven Dependencies**
66
```xml
@@ -247,7 +247,7 @@ Employee id 2 -> EmployeeEntity [id=2, firstName=Deja, lastName=Vu, email=xyz@em
247247
<b><a href="#">↥ back to top</a></b>
248248
</div>
249249

250-
#### Q. Mention the differences between JPA and Hibernate?
250+
## Q. Mention the differences between JPA and Hibernate?
251251
JPA is a specification for accessing, persisting and managing the data between Java objects and the relational database.
252252

253253
Where as, Hibernate is the actual implementation of JPA guidelines. When hibernate implements the JPA specification, this will be certified by the JPA group upon following all the standards mentioned in the specification. For example, JPA guidelines would provide information of mandatory and optional features to be implemented as part of the JPA implementation.
@@ -260,7 +260,7 @@ Where as, Hibernate is the actual implementation of JPA guidelines. When hiberna
260260
|It is used to map Java data types with database tables and SQL data types.|It is the standard API which allows developers to perform database operations smoothly.|
261261
|The Query language in this is Hibernate Query Language.|The query language of JPA is JPQL (Java Persistence Query Language)|
262262

263-
#### Q. What is HQL and what are its benefits?
263+
## Q. What is HQL and what are its benefits?
264264
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.
265265

266266
**Advantages Of HQL:**
@@ -360,7 +360,7 @@ List results = query.list();
360360
<b><a href="#">↥ back to top</a></b>
361361
</div>
362362

363-
#### Q. Mention the Key components of Hibernate?
363+
## Q. Mention the Key components of Hibernate?
364364

365365
**hibernate.cfg.xml**: This file has database connection details
366366

@@ -377,7 +377,7 @@ List results = query.list();
377377
* Session represents unit of work with database
378378
* Session should be closed once the task is completed
379379

380-
#### Q. Explain Session object in Hibernate?
380+
## Q. Explain Session object in Hibernate?
381381
A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.
382382

383383
The lifecycle of a Session is bounded by the beginning and end of a logical transaction. The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:
@@ -410,7 +410,7 @@ catch (Exception e) {
410410
<b><a href="#">↥ back to top</a></b>
411411
</div>
412412

413-
#### Q. How transaction management works in Hibernate?
413+
## Q. How transaction management works in Hibernate?
414414
A **Transaction** is a sequence of operation which works as an atomic unit. A transaction only completes if all the operations completed successfully. A transaction has the Atomicity, Consistency, Isolation, and Durability properties (ACID).
415415

416416
In hibernate framework, **Transaction interface** that defines the unit of work. It maintains abstraction from the transaction implementation (JTA, JDBC). A transaction is associated with Session and instantiated by calling **session.beginTransaction()**.
@@ -446,7 +446,7 @@ try {
446446
sessionObj.close();
447447
}
448448
```
449-
#### Q. Explain the Criteria object in Hibernate?
449+
## Q. Explain the Criteria object in Hibernate?
450450
The Criteria API allows to build up a criteria query object programmatically; the `org.hibernate.Criteria` interface defines the available methods for one of these objects. The Hibernate Session interface contains several overloaded `createCriteria()` methods.
451451

452452
**1. Restrictions with Criteria**
@@ -791,7 +791,7 @@ Total Salary: 15000
791791
<b><a href="#">↥ back to top</a></b>
792792
</div>
793793

794-
#### Q. What is a One-to-One association in Hibernate?
794+
## Q. What is a One-to-One association in Hibernate?
795795
A **One-to-One** Association is similar to Many-to-One association with a difference that the column will be set as unique i.e. Two entities are said to be in a One-to-One relationship if one entity has only one occurrence in the other entity. For example, an address object can be associated with a single employee object. However, these relationships are rarely used in the relational table models and therefore, we won’t need this mapping too often.
796796

797797
In One-to-One association, the source entity has a field that references another target entity. The `@OneToOne` JPA annotation is used to map the source entity with the target entity.
@@ -1050,7 +1050,7 @@ Closing SessionFactory
10501050
<b><a href="#">↥ back to top</a></b>
10511051
</div>
10521052

1053-
#### Q. What is hibernate caching? Explain Hibernate first level cache?
1053+
## Q. What is hibernate caching? Explain Hibernate first level cache?
10541054
Hibernate Cache can be very useful in gaining fast application performance if used correctly. The idea behind cache is to reduce the number of database queries, hence reducing the throughput time of the application.
10551055

10561056
Hibernate comes with different types of Cache:
@@ -1161,7 +1161,7 @@ Session Contains Employee with id=2?true
11611161
<b><a href="#">↥ back to top</a></b>
11621162
</div>
11631163

1164-
#### Q. What is second level cache in Hibernate?
1164+
## Q. What is second level cache in Hibernate?
11651165
**Hibernate second level cache** uses a common cache for all the session object of a session factory. It is useful if you have multiple session objects from a session factory. **SessionFactory** holds the second level cache data. It is global for all the session objects and not enabled by default.
11661166

11671167
Different vendors have provided the implementation of Second Level Cache.
@@ -1314,7 +1314,7 @@ public class FetchTest {
13141314
<b><a href="#">↥ back to top</a></b>
13151315
</div>
13161316

1317-
#### Q. What are concurrency strategies?
1317+
## Q. What are concurrency strategies?
13181318
The READ_WRITE strategy is an asynchronous cache concurrency mechanism and to prevent data integrity issues (e.g. stale cache entries), it uses a locking mechanism that provides unit-of-work isolation guarantees.
13191319

13201320
In hibernate, cache concurrency strategy can be set globally using the property hibernate.cache. default_cache_concurrency_strategy. The allowed values are,
@@ -1378,15 +1378,15 @@ public void doAfterTransactionCompletion(boolean success,
13781378
<b><a href="#">↥ back to top</a></b>
13791379
</div>
13801380

1381-
#### Q. What is Lazy loading in hibernate?
1381+
## Q. What is Lazy loading in hibernate?
13821382
Hibernate defaults to a lazy fetching strategy for all entities and collections. Lazy loading in hibernate improves the performance. It loads the child objects on demand. To enable lazy loading explicitly you must use **fetch = FetchType.LAZY** on a association which you want to lazy load when you are using hibernate annotations.
13831383

13841384
Example:
13851385
```java
13861386
@OneToMany( mappedBy = "category", fetch = FetchType.LAZY )
13871387
private Set<ProductEntity> products;
13881388
```
1389-
#### Q. Explain the persistent classes in Hibernate?
1389+
## Q. Explain the persistent classes in Hibernate?
13901390
Persistence class are simple POJO classes in an application. It works as implementation of the business application for example Employee, department etc. It is not necessary that all instances of persistence class are defined persistence.
13911391

13921392
There are following main rules of persistent classes
@@ -1458,7 +1458,7 @@ public class Employee {
14581458
<b><a href="#">↥ back to top</a></b>
14591459
</div>
14601460

1461-
#### Q. Explain some of the elements of hbm.xml?
1461+
## Q. Explain some of the elements of hbm.xml?
14621462
Hibernate mapping file is used by hibernate framework to get the information about the mapping of a POJO class and a database table.
14631463

14641464
It mainly contains the following mapping information:
@@ -1489,7 +1489,7 @@ Syntax
14891489

14901490
</hibernate-mapping>
14911491
```
1492-
#### Q. What is Java Persistence API (JPA)?
1492+
## Q. What is Java Persistence API (JPA)?
14931493
Java Persistence API is a collection of classes and methods to persistently store the vast amounts of data into a database.
14941494
The Java Persistence API (JPA) is one possible approach to ORM. Via JPA the developer can map, store, update and retrieve data from relational databases to Java objects and vice versa. JPA can be used in Java-EE and Java-SE applications.
14951495

@@ -1523,7 +1523,7 @@ Relationship annotations:
15231523
<b><a href="#">↥ back to top</a></b>
15241524
</div>
15251525

1526-
#### Q. Name some important interfaces of Hibernate framework?
1526+
## Q. Name some important interfaces of Hibernate framework?
15271527
* **Session Interface**: This is the primary interface used by hibernate applications
15281528
The instances of this interface are lightweight and are inexpensive to create and destroy
15291529
Hibernate sessions are not thread safe
@@ -1537,7 +1537,7 @@ The instance of this interface is used by the application in order to specify th
15371537

15381538
* **Query and Criteria Interface**: This interface allows the user to perform queries and also control the flow of the query execution
15391539

1540-
#### Q. What is Hibernate SessionFactory and how to configure it?
1540+
## Q. What is Hibernate SessionFactory and how to configure it?
15411541
SessionFactory can be created by providing Configuration object, which will contain all DB related property details pulled from either hibernate.cfg.xml file or hibernate.properties file. SessionFactory is a factory for Session objects.
15421542

15431543
We can create one SessionFactory implementation per database in any application. If your application is referring to multiple databases, then we need to create one SessionFactory per database.
@@ -1595,7 +1595,7 @@ public class HibernateUtil {
15951595
<b><a href="#">↥ back to top</a></b>
15961596
</div>
15971597

1598-
#### Q. What is the difference between opensession and getcurrentsession in hibernate?
1598+
## Q. What is the difference between opensession and getcurrentsession in hibernate?
15991599
Hibernate SessionFactory getCurrentSession() method returns the session bound to the context. Since this session object belongs to the hibernate context, we don't need to close it. Once the session factory is closed, this session object gets closed.
16001600

16011601
Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations.
@@ -1653,7 +1653,7 @@ try {
16531653
<b><a href="#">↥ back to top</a></b>
16541654
</div>
16551655

1656-
#### Q. What is difference between Hibernate Session get() and load() method?
1656+
## Q. What is difference between Hibernate Session get() and load() method?
16571657
Hibernate Session class provides two method to access object e.g. `session.get()` and `session.load()`.The difference between get() vs load() method is that get() involves database hit if object doesn't exists in **Session Cache** and returns a fully initialized object which may involve several database call while load method can return proxy in place and only initialize the object or hit the database if any method other than getId() is called on persistent or entity object. This lazy initialization can save couple of database round-trip which result in better performance.
16581658

16591659
|load() |get()
@@ -1662,7 +1662,7 @@ Hibernate Session class provides two method to access object e.g. `session.get()
16621662
|load() method will throw an exception if the unique id is not found in the database.|get() method will return null if the unique id is not found in the database.|
16631663
|load() just returns a proxy by default and database won't be hit until the proxy is first invoked.|get() will hit the database immediately.|
16641664

1665-
#### Q. What are different states of an entity bean?
1665+
## Q. What are different states of an entity bean?
16661666
The Entity bean has three states:
16671667

16681668
**1. Transient**: Transient objects exist in heap memory. Hibernate does not manage transient objects or persist changes to transient objects. Whenever pojo class object created then it will be in the Transient state.
@@ -1733,7 +1733,7 @@ mysql> select * from student;
17331733
<b><a href="#">↥ back to top</a></b>
17341734
</div>
17351735

1736-
#### Q. What is difference between merge() and update() methods in Hibernate?
1736+
## Q. What is difference between merge() and update() methods in Hibernate?
17371737
Both update() and merge() methods in hibernate are used to convert the object which is in detached state into persistence state. But there are different situation where we should be used update() and where should be used merge() method in hibernate
17381738

17391739
```java
@@ -1848,6 +1848,7 @@ In the hibernate session we can maintain only one employee object in persistent
18481848
#### Q. What are the inheritance mapping strategies?
18491849
#### Q. What is automatic dirty checking in hibernate?
18501850
#### Q. Explain Hibernate configuration file and Hibernate mapping file?
1851+
18511852
<div align="right">
18521853
<b><a href="#">↥ back to top</a></b>
18531854
</div>

0 commit comments

Comments
 (0)