Crucial Hibernate Interview Questions

Q6: Explain the Many-to-Many association in Hibernate?

Ans:Just like the previous type of association, Many-to-Many mapping needs an entity attribute. In this association, @ManyToMany annotation is used. In terms of direction, it has two types: unidirectional and bidirectional. Furthermore, you see two sides: the owning side and the inverse side. The attributes model the association in the unidirectional type. So, you can utilize it for navigation in your domain model or JPQL queries.

In hibernate, the annotation is used to map a Many-to-Many association. As it is already clear from its name, the bidirectional relationship helps to navigate the association in both directions. It is possible to map this relation by using either list, set, bag, map, etc.

Q7: How can you integrate Hibernate and Spring?

Ans:Today, Spring is a Java framework that is frequently used in the market. The difference between Spring and Hibernate is that Spring is a JavaEE Framework, whereas, Hibernate is an ORM framework. Due to that, Spring and Hibernate are used in combination for many enterprise applications.

If you want to integrate them, you must follow some steps. To learn these steps in the simplest language, read the pointers that are mentioned:

  • First of all, add Hibernate-entity manager, Hibernate-core and Spring-ORM dependencies.
  • To perform the database operation, create Model classes and corresponding DAO implementations. The DAO classes utilize the SessionFactory that is injected with the help of the Spring Bean configuration.

Remember, in such operations, you don’t need to use the Hibernate Transaction. Because the declarative transaction management by Spring takes care of that, you only need to use the @Transactional annotation.

Q8: What do you mean by Hibernate Configuration File?

Ans: The Hibernate Configuration File has database-specific configurations. These configurations have a role in initializing the SessionFactory. When you work with hibernate, Hibernate Configuration File(cfg file) is that file that is loaded into a hibernate application. In order to make a connection to the database server, hibernate uses this file.

The components that are essential in the Hibernate Configuration File include Dialect information. Consequently, hibernate knows the type of database and mapping file or the details of a class. Mostly, this information is transferred as a standard Java properties file. Also, it is known as, or as an XML file with the name hibernate.cfg.xml.

Q9: Define Session and the method to use it in Hibernate?

Ans: Basically, Hibernate Session is an interface between Hibernate and Java application layer. It has the ability to establish a physical association with the database. The lightweight Session object is generated. It instantiates every time an interaction is required with the database. Because of the hibernate session and the process that takes place during it, the persistent objects are stored. Also, they can be retrieved via Session object.

This Session gives methods to produce, read, update and remove operations for a constant object. If you plan to get the Session, with the use of the Session object, you have to execute HQL queries and SQL native queries.

Q10: What is Hibernate SessionFactory?

Ans: In the org. hibernate package, an interface exists that is known as SessionFactory. It is important to know about it because it makes the Session Objects. It can’t be altered over time and has thread-safe nature. By that, we mean it can be used by each and every thread of an application. The buildSessionFactory() technique combines the meta-data present in the cfg Object.

The SessionFactory is an object that is pretty heavy in weight. Hence, at first, it is created during application startup. Later on, it is held up there for future use. You can say that the SessionFactory is the factory class that allows you to get the Session objects.