Hibernate Interview Questions and Answers
Question - 41 : - Why use Hibernate Framework?
Answer - 41 : -
Hibernate overcomes the shortcomings of other technologies like JDBC.
- It overcomes the database dependency faced in the JDBC.
- Changing of the databases cost a lot working on JDBC, hibernate overcomes this problem with flying colors.
- Code portability is not an option while working on JDBC. This is easily handled by Hibernate.
- Hibernate strengthens the object level relationship.
- It overcomes the exception-handling part which is mandatory while working on JDBC.
It reduces the length of code with increased readability by overcoming the boilerplate problem.
Question - 42 : - What are the different functionalities supported by Hibernate?
Answer - 42 : -
- Hibernate is an ORM tool.
- Hibernate uses Hibernate Query Language(HQL) which makes it database-independent.
- It supports auto DDL operations.
- This Java framework also has an Auto Primary Key Generation support.
- Supports cache memory.
- Exception handling is not mandatory in the case of Hibernate.
Question - 43 : - What are the technologies that are supported by Hibernate?
Answer - 43 : -
Hibernate supports a variety of technologies, like:
- XDoclet Spring
- Maven
- Eclipse Plug-ins
- J2EE
Question - 44 : - What is HQL?
Answer - 44 : -
HQL is the acronym of Hibernate Query Language. It is an Object-Oriented Query Language and is independent of the database.
Question - 45 : - How to achieve mapping in Hibernate?
Answer - 45 : -
Association mappings are one of the key features of Hibernate. It supports the same associations as the relational database model. They are:
- One-to-One associations
- Many-to-One associations
- Many-to-Many associations
Question - 46 : - Name some of the important interfaces of Hibernate framework?
Answer - 46 : -
Hibernate interfaces are:
- SessionFactory (org.hibernate.SessionFactory)
- Session (org.hibernate.Session)
- Transaction (org.hibernate.Transaction)
Question - 47 : - What is One-to-One association in Hibernate?
Answer - 47 : -
In this type of mapping, you only need to model the system for the entity for which you want to navigate the relationship in your query or domain model. You need an entity attribute that represents the association, so annotate it with an @OneToOne annotation.
Question - 48 : - What is One-to-Many association in Hibernate?
Answer - 48 : -
In this type of association, one object can be associated with multiple/different objects. Talking about the mapping, the One-to-Many mapping is implemented using a Set Java collection that does not have any redundant element. This One-to-Many element of the set indicates the relation of one object to multiple objects.
Question - 49 : - What is Many-to-Many association in Hibernate?
Answer - 49 : -
Many-to-Many mapping requires an entity attribute and a @ManyToMany annotation. It can either be unidirectional and bidirectional. In Unidirectional, the attributes model the association and you can use it to navigate it in your domain model or JPQL queries. The annotation tells Hibernate to map a Many-to-Many association. The bidirectional relationship, mapping allows you to navigate the association in both directions.
Question - 50 : - How to integrate Hibernate and Spring?
Answer - 50 : -
Spring is also one of the most commonly used Java frameworks in the market today. Spring is a JavaEE Framework and Hibernate is the most popular ORM framework. This is why Spring Hibernate combination is used in a lot of enterprise applications.
Following are the steps you should follow to integrate Spring and Hibernate.
- Add Hibernate-entity manager, Hibernate-core and Spring-ORM dependencies.
- Create Model classes and corresponding DAO implementations for database operations. The DAO classes will use SessionFactory that will be injected by the Spring Bean configuration.
- Note that you don’t need to use Hibernate Transaction Management, as you can leave it to the Spring declarative transaction management using @Transactional annotation.