Preparing for the Java EE interview and you don’t know what questions will make you crack the interview? Don’t worry, we’ve compiled a list of interview questions that are usually asked and make you get the desired job with ease. Read the article carefully.
What is the Hibernate Framework?
Hibernate is a library for the Java programming language intended for solving object-relational mapping (ORM) problems. It is a free open source software distributed under the terms of the GNU Lesser General Public License. This library provides an easy-to-use framework (framework) for mapping the object-oriented data model to traditional relational databases. Hibernate is compatible with JSR-220/317 and offers standard JPA tools.
What are the important benefits of using the Hibernate Framework?
Hibernate is one of the most popular ORM frameworks for Java. And that’s why:
Hibernate eliminates a lot of spaghetti code (repetitive), which constantly harasses the developer when working with JDBC. Hides from the developer a lot of code needed to manage resources and allows you to focus on business logic.
Hibernate supports XML as well as JPA annotations, which implements the code independently.
Hibernate provides its powerful query language (HQL), which is similar to SQL. It’s worth noting that HQL is fully object-oriented and understands principles such as inheritance, polymorphism, and associations (links).
Hibernate is a widely distributed open source project. Thanks to this, thousands of open articles, examples, as well as documentation for using the framework are available.
Hibernate easily integrates with other Java EE frameworks, for example; the Spring Framework supports built-in integration with Hibernate. Hibernate supports lazy initialization using proxy objects and performs queries against the database only when necessary. It is important that Hibernate can use pure SQL, which means it supports the ability to optimize queries and work with any third-party database vendor and its features.
What are the advantages of Hibernate over JDBC?
Hibernate has several advantages over the JDBC API:
- Hibernate removes a lot of duplicate code from the JDBC API, and therefore it is easier to read, write and maintain.
- Hibernate supports inheritance, associations, and collections, which is not available in the JDBC API.
- Hibernate implicitly uses transaction management. Most queries cannot be executed outside of the transaction. When using the JDBC API for transaction management, you must explicitly use commit and rollback.
- JDBC API throws SQLException, which refers to the checked exceptions, and therefore it is necessary to write a lot of try-catch blocks constantly. In most cases, this is not necessary for every JDBC call and is used to manage transactions. Hibernate wraps JDBC exceptions through an unchecked JDBCException or HibernateException, so there’s no need to check them in the code every time. The built-in support for transaction management in Hibernate removes try-catch blocks.
- Hibernate Query Language (HQL) is more object-oriented and closer to Java query language than SQL in JDBC.
- Hibernate supports caching, and JDBC requests do not, which can reduce performance.
- The Hibernate configuration allows you to use JDBC, such as a JNDI DataSource connection for a connection pool. This is an important feature for enterprise applications, which is completely missing from the JDBC API.
- Hibernate supports JPA annotations, so the code is portable to other ORM frameworks that implement the standard, while the JDBC code is strongly tied to the application.
Name some important Hibernate interfaces.
SessionFactory (org.hibernate.SessionFactory) is an immutable thread-safe object with compiled mapping for one database. It is necessary to initialize SessionFactory only once. A SessionFactory instance is used to obtain Session objects that are used for database operations.
Session (org.hibernate.Session) is a single-threaded short-lived object that provides a link between application objects and the database. It wraps JDBC java.sql.Connection and works as a factory for org.hibernate.Transaction. The developer must open the session as necessary and close it immediately after use. The Session instance is the interface between the code in the java application and the hibernate framework and provides methods for CRUD operations.
Transaction (org.hibernate.Transaction) is a single-threaded short-lived object used for atomic operations. This is an abstraction of the application from the basic JDBC or JTA transactions. org.hibernate.The session can occupy several org.hibernate.Transaction in certain cases.
What is the Hibernate configuration file?
The Hibernate configuration file contains data about the database and is required to initialize the SessionFactory. In a .xml file, you must specify the vendor database or JNDI resources, as well as information about the dialect used, which will help hibernate choose the mode of working with a particular database.
What is Hibernate mapping file?
A mapping file is used to link entity beans and columns in a database table. In cases where the JPA annotations are not used, the .xml display file can be useful (for example, when using third-party libraries).
Name some essential annotations used to display in Hibernate.
Hibernate supports both annotations from JPA, and their own, which are in the package org.hibernate.annotations. The most important annotations of JPA and Hibernate are:
- persistence.Entity: Used to specify a class as an entity bean.
- persistence.Table: Used to specify the table name from the database to be mapped to the entity bean.
- persistence.Access: Specifies the access type, field, or property. The field is the default value, and if you want to hibernate to use the getter/setter methods, you must set them for the desired property.
- persistence.Id: defines the primary key in the entity bean.
- persistence.EmbeddedId: Used to define a composite key in a bean.
- persistence.Column: Specifies the column name from the table in the database.
- persistence.GeneratedValue: Specifies the strategy for creating the primary keys. Used in conjunction with javax.persistence.GenerationType enum.
- persistence.OneToOne: Specifies a one-to-one relationship between two intrinsic beans. Accordingly, there are other annotations of OneToMany, ManyToOne and ManyToMany.
- hibernate.annotations.Cascade: defines a cascading relationship between two entity beans. Used in conjunction with org.hibernate.annotations.CascadeType .
- persistence.PrimaryKeyJoinColumn: Specifies the foreign key for the property.
What do you know about Hibernate SessionFactory and how to configure it?
SessionFactory is a factory of classes and is used to obtain session objects. SessionFactory is responsible for reading the Hibernate configuration parameters and connecting to the database. Typically, there is only one SessionFactory instance in the application, and the threads serving the client requests receive session instances using the SessionFactory object. The internal state of the SessionFactory is immutable. Internal state includes all the metadata about Object / Relational Mapping and is specified when the SessionFactory is created.
SessionFactory also provides methods for obtaining class metadata and statistics, such as data about the second level of the cache, queries executed, etc.
Is the Hibernate SessionFactory thread-safe?
Because the SessionFactory object is immutable, then yes, it’s thread-safe. Multiple threads can access the same object at the same time.
How to get Hibernate Session and what is it?
The Hibernate Session object is the connection between the application’s Java code and hibernate. This is the main interface for performing operations with the database. The life cycle of the session object is related to the beginning and the end of the transaction. This object provides methods for CRUD (create, read, update, delete) operations for the persistence object. With this instance, you can perform HQL, SQL queries, and set sample criteria.