8. Hibernate Interview Questions
|| Hibernate Interview Question ||
1. What is ORM in Hibernate?
Hibernate ORM stands for Object Relational Mapping. This is a mapping tool pattern mainly used for converting data stored in a relational database to an object used in object-oriented programming constructs. This tool also helps greatly in simplifying data retrieval, creation, and manipulation.
2. What are the advantages of Hibernate over JDBC?
- The advantages of Hibernate over JDBC are listed below:
- Clean Readable Code: Using hibernate, helps in eliminating a lot of JDBC API-based boiler-plate codes, thereby making the code look cleaner and readable.
- HQL (Hibernate Query Language): Hibernate provides HQL which is closer to Java and is object-oriented in nature. This helps in reducing the burden on developers for writing database independent queries. In JDBC, this is not the case. A developer has to know the database-specific codes.
- Transaction Management: JDBC doesn't support implicit transaction management. It is upon the developer to write transaction management code using commit and rollback methods. Whereas, Hibernate implicity provides this feature.
- Exception Handling: Hibernate wraps the JDBC exceptions and throws unchecked exceptions like JDBCException or HibernateException. This along with the built-in transaction management system helps developers to avoid writing multiple try-catch blocks to handle exceptions. In the case of JDBC, it throws a checked exception called SQLException thereby mandating the developer to write try-catch blocks to handle this exception at compile time.
- Special Features: Hibernate supports OOPs features like inheritance, associations and also supports collections. These are not available in JDBC.
3. What are some of the important interfaces of Hibernate framework?
Hibernate core interfaces are:
Configuration
SessionFactory
Session
Criteria
Query
Transaction
4. What is a Session in Hibernate?
A session is an object that maintains the connection between Java object application and database. Session also has methods for storing, retrieving, modifying or deleting data from database using methods like persist(), load(), get(), update(), delete(), etc. Additionally, It has factory methods to return Query, Criteria, and Transaction objects.
5. What is a SessionFactory?
SessionFactory provides an instance of Session. It is a factory class that gives the Session objects based on the configuration parameters in order to establish the connection to the database.
As a good practice, the application generally has a single instance of SessionFactory. The internal state of a SessionFactory which includes metadata about ORM is immutable, i.e once the instance is created, it cannot be changed.
This also provides the facility to get information like statistics and metadata related to a class, query executions, etc. It also holds second-level cache data if enabled.
6. What do you think about the statement - “session being a thread-safe object”?
No, Session is not a thread-safe object which means that any number of threads can access data from it simultaneously.
7. Can you explain what is lazy loading in hibernate?
Lazy loading is mainly used for improving the application performance by helping to load the child objects on demand.
It is to be noted that, since Hibernate 3 version, this feature has been enabled by default. This signifies that child objects are not loaded until the parent gets loaded.
8. What is the difference between first level cache and second level cache?
Hibernate has 2 cache types. First level and second level cache for which the difference is given below:
First Level Cache
- This is local to the Session object and cannot be shared between multiple sessions.
- This cache is enabled by default and there is no way to disable it.
- The first level cache is available only until the session is open, once the session is closed, the first level cache is destroyed.
Second Level Cache
- This cache is maintained at the SessionFactory level and shared among all sessions in Hibernate.
- This is disabled by default, but we can enable it through configuration.
- The second-level cache is available through the application’s life cycle, it is only destroyed and recreated when an application is restarted.
If an entity or object is loaded by calling the get() method then Hibernate first checked the first level cache, if it doesn’t find the object then it goes to the second level cache if configured. If the object is not found then it finally goes to the database and returns the object, if there is no corresponding row in the table then it returns null.
9. What can you tell about Hibernate Configuration File?
Hibernate Configuration File or hibernate.cfg.xml is one of the most required configuration files in Hibernate. By default, this file is placed under the src/main/resource folder.
The file contains database related configurations and session-related configurations.
Hibernate facilitates providing the configuration either in an XML file (like hibernate.cfg.xml) or a properties file (like hibernate.properties).
This file is used to define the below information:
Database connection details: Driver class, URL, username, and password.
There must be one configuration file for each database used in the application, suppose if we want to connect with 2 databases, then we must create 2 configuration files with different names.
Hibernate properties: Dialect, show_sql, second_level_cache, and mapping file names.
10. How do you create an immutable class in hibernate?
Immutable class in hibernate creation could be in the following way. If we are using the XML form of configuration, then a class can be made immutable by markingmutable=false. The default value is true there which indicating that the class was not created by default.
In the case of using annotations, immutable classes in hibernate can also be created by using @Immutable annotation.
11. Can you explain the concept behind Hibernate Inheritance Mapping?
Java is an Object-Oriented Programming Language and Inheritance is one of the most important pillars of object-oriented principles. To represent any models in Java, inheritance is most commonly used to simplify and simplify the relationship. But, there is a catch. Relational databases do not support inheritance. They have a flat structure.
Hibernate’s Inheritance Mapping strategies deal with solving how to hibernate being an ORM tries to map this problem between the inheritance of Java and flat structure of Databases.
There are different inheritance mapping strategies available:
Single Table Strategy
Table Per Class Strategy
Mapped Super Class Strategy
Joined Table Strategy
Comments
Post a Comment