Hibernate Interview Questions and Answers
Question - 71 : - How can we see Hibernate generated SQL on console?
Answer - 71 : -
In order to view the SQL on a console, you need to add following in Hibernate configuration file to enable viewing SQL on the console for debugging purposes:
Question - 72 : - What is Query Cache in Hibernate?
Answer - 72 : -
Hibernate implements a separate cache region for queries resultset that integrates with the Hibernate second-level cache. This is also an optional feature and requires a few more steps in code.
Question - 73 : - What is the benefit of Native SQL query support in Hibernate?
Answer - 73 : -
Hibernate provides an option to execute Native SQL queries through the use of the SQLQuery object. For normal scenarios, it is however not the recommended approach because you might lose other benefits like Association and Hibernate first-level caching.
Native SQL Query comes handy when you want to execute database-specific queries that are not supported by Hibernate API such query hints or the Connect keyword in Oracle Database.
Question - 74 : - What is Named SQL Query?
Answer - 74 : -
Hibernate provides another important feature called Named Query using which you can define at a central location and use them anywhere in the code.
You can create named queries for both HQL as well as for Native SQL. These Named Queries can be defined in Hibernate mapping files with the help of JPA annotations @NamedQuery and @NamedNativeQuery.
Question - 75 : - When do you use merge() and update() in Hibernate?
Answer - 75 : -
This is one of the tricky Hibernate Interview Questions asked.
- update(): If you are sure that the Hibernate Session does not contain an already persistent instance with the same id .
- merge(): Helps in merging your modifications at any time without considering the state of the Session.
Question - 76 : - Difference between get() vs load() method in Hibernate?
Answer - 76 : -
This is one of the most frequently asked Hibernate Interview Questions. The key difference between the get() and load() method is:
load(): It will throw an exception if an object with an ID passed to them is not found.
get(): Will return null.
load(): It can return proxy without hitting the database unless required.
get(): It always goes to the database.
So sometimes using load() can be faster than the get() method.
Question - 77 : - Difference between the first and second level cache in Hibernate?
Answer - 77 : -
The first-level cache is maintained at Session level while the second level cache is maintained at a SessionFactory level and is shared by all sessions.
Question - 78 : - Difference between Session and SessionFactory in Hibernate?
Answer - 78 : -
This is yet another popular Hibernate Interview Question asked.
- A Session is a single-threaded, short-lived object. It provides the first-level cache.
- SessionFactory is immutable and shared by all Session. It also lives until the Hibernate is running. It also provides the second-level cache.
Question - 79 : - What are the best practices that Hibernate recommends for persistent classes?
Answer - 79 : -
- All Java classes that will be persisted need a default constructor.
- All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database. This property maps to the primary key column of a database table.
- All attributes that will be persisted should be declared private and have getXXX and setXXX methods defined in the JavaBean style.
- A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.
- All classes that do not extend or implement some specialized classes and interfaces required by the EJB framework.
Question - 80 : - Difference between managed associations and Hibernate associations?
Answer - 80 : -
Managed associations: Relate to container management persistence and are bi-directional.
Hibernate Associations: These associations are unidirectional.