You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
####Q. Mention the differences between JPA and Hibernate?
250
+
## Q. Mention the differences between JPA and Hibernate?
251
251
JPA is a specification for accessing, persisting and managing the data between Java objects and the relational database.
252
252
253
253
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
260
260
|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.|
261
261
|The Query language in this is Hibernate Query Language.|The query language of JPA is JPQL (Java Persistence Query Language)|
262
262
263
-
####Q. What is HQL and what are its benefits?
263
+
## Q. What is HQL and what are its benefits?
264
264
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.
265
265
266
266
**Advantages Of HQL:**
@@ -360,7 +360,7 @@ List results = query.list();
360
360
<b><a href="#">↥ back to top</a></b>
361
361
</div>
362
362
363
-
####Q. Mention the Key components of Hibernate?
363
+
## Q. Mention the Key components of Hibernate?
364
364
365
365
**hibernate.cfg.xml**: This file has database connection details
366
366
@@ -377,7 +377,7 @@ List results = query.list();
377
377
* Session represents unit of work with database
378
378
* Session should be closed once the task is completed
379
379
380
-
####Q. Explain Session object in Hibernate?
380
+
## Q. Explain Session object in Hibernate?
381
381
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.
382
382
383
383
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) {
410
410
<b><a href="#">↥ back to top</a></b>
411
411
</div>
412
412
413
-
####Q. How transaction management works in Hibernate?
413
+
## Q. How transaction management works in Hibernate?
414
414
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).
415
415
416
416
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 {
446
446
sessionObj.close();
447
447
}
448
448
```
449
-
####Q. Explain the Criteria object in Hibernate?
449
+
## Q. Explain the Criteria object in Hibernate?
450
450
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.
451
451
452
452
**1. Restrictions with Criteria**
@@ -791,7 +791,7 @@ Total Salary: 15000
791
791
<b><a href="#">↥ back to top</a></b>
792
792
</div>
793
793
794
-
####Q. What is a One-to-One association in Hibernate?
794
+
## Q. What is a One-to-One association in Hibernate?
795
795
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.
796
796
797
797
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
1050
1050
<b><a href="#">↥ back to top</a></b>
1051
1051
</div>
1052
1052
1053
-
####Q. What is hibernate caching? Explain Hibernate first level cache?
1053
+
## Q. What is hibernate caching? Explain Hibernate first level cache?
1054
1054
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.
1055
1055
1056
1056
Hibernate comes with different types of Cache:
@@ -1161,7 +1161,7 @@ Session Contains Employee with id=2?true
1161
1161
<b><a href="#">↥ back to top</a></b>
1162
1162
</div>
1163
1163
1164
-
####Q. What is second level cache in Hibernate?
1164
+
## Q. What is second level cache in Hibernate?
1165
1165
**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.
1166
1166
1167
1167
Different vendors have provided the implementation of Second Level Cache.
@@ -1314,7 +1314,7 @@ public class FetchTest {
1314
1314
<b><a href="#">↥ back to top</a></b>
1315
1315
</div>
1316
1316
1317
-
####Q. What are concurrency strategies?
1317
+
## Q. What are concurrency strategies?
1318
1318
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.
1319
1319
1320
1320
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,
1378
1378
<b><a href="#">↥ back to top</a></b>
1379
1379
</div>
1380
1380
1381
-
####Q. What is Lazy loading in hibernate?
1381
+
## Q. What is Lazy loading in hibernate?
1382
1382
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.
####Q. Explain the persistent classes in Hibernate?
1389
+
## Q. Explain the persistent classes in Hibernate?
1390
1390
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.
1391
1391
1392
1392
There are following main rules of persistent classes
@@ -1458,7 +1458,7 @@ public class Employee {
1458
1458
<b><a href="#">↥ back to top</a></b>
1459
1459
</div>
1460
1460
1461
-
####Q. Explain some of the elements of hbm.xml?
1461
+
## Q. Explain some of the elements of hbm.xml?
1462
1462
Hibernate mapping file is used by hibernate framework to get the information about the mapping of a POJO class and a database table.
1463
1463
1464
1464
It mainly contains the following mapping information:
@@ -1489,7 +1489,7 @@ Syntax
1489
1489
1490
1490
</hibernate-mapping>
1491
1491
```
1492
-
####Q. What is Java Persistence API (JPA)?
1492
+
## Q. What is Java Persistence API (JPA)?
1493
1493
Java Persistence API is a collection of classes and methods to persistently store the vast amounts of data into a database.
1494
1494
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.
1495
1495
@@ -1523,7 +1523,7 @@ Relationship annotations:
1523
1523
<b><a href="#">↥ back to top</a></b>
1524
1524
</div>
1525
1525
1526
-
####Q. Name some important interfaces of Hibernate framework?
1526
+
## Q. Name some important interfaces of Hibernate framework?
1527
1527
***Session Interface**: This is the primary interface used by hibernate applications
1528
1528
The instances of this interface are lightweight and are inexpensive to create and destroy
1529
1529
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
1537
1537
1538
1538
***Query and Criteria Interface**: This interface allows the user to perform queries and also control the flow of the query execution
1539
1539
1540
-
####Q. What is Hibernate SessionFactory and how to configure it?
1540
+
## Q. What is Hibernate SessionFactory and how to configure it?
1541
1541
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.
1542
1542
1543
1543
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 {
1595
1595
<b><a href="#">↥ back to top</a></b>
1596
1596
</div>
1597
1597
1598
-
####Q. What is the difference between opensession and getcurrentsession in hibernate?
1598
+
## Q. What is the difference between opensession and getcurrentsession in hibernate?
1599
1599
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.
1600
1600
1601
1601
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 {
1653
1653
<b><a href="#">↥ back to top</a></b>
1654
1654
</div>
1655
1655
1656
-
####Q. What is difference between Hibernate Session get() and load() method?
1656
+
## Q. What is difference between Hibernate Session get() and load() method?
1657
1657
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.
1658
1658
1659
1659
|load() |get()
@@ -1662,7 +1662,7 @@ Hibernate Session class provides two method to access object e.g. `session.get()
1662
1662
|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.|
1663
1663
|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.|
1664
1664
1665
-
####Q. What are different states of an entity bean?
1665
+
## Q. What are different states of an entity bean?
1666
1666
The Entity bean has three states:
1667
1667
1668
1668
**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;
1733
1733
<b><a href="#">↥ back to top</a></b>
1734
1734
</div>
1735
1735
1736
-
####Q. What is difference between merge() and update() methods in Hibernate?
1736
+
## Q. What is difference between merge() and update() methods in Hibernate?
1737
1737
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
1738
1738
1739
1739
```java
@@ -1848,6 +1848,7 @@ In the hibernate session we can maintain only one employee object in persistent
1848
1848
#### Q. What are the inheritance mapping strategies?
1849
1849
#### Q. What is automatic dirty checking in hibernate?
1850
1850
#### Q. Explain Hibernate configuration file and Hibernate mapping file?
0 commit comments