Home APPS Development Gaming Mobile TECHNOLOGY Software Hordware OTHERS Multiple Choice Question (MCQ) Login

Hibernate – Java Interview Questions

Categories: OTHERS

Q.1. What is Hibernate Framework?

 

Ans. Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is Java-based ORM tool that provides a framework for mapping application domain objects to the relational database tables and vice versa.

Hibernate provides a reference implementation of Java Persistence API, that makes it a great choice as ORM tool with benefits of loose coupling. We can use the Hibernate persistence API for CRUD operations. Hibernate framework provide option to map plain old java objects to traditional database tables with the use of JPA annotations as well as XML based configuration.

Similarly, hibernate configurations are flexible and can be done from XML configuration file as well as programmatically.

 

Q.2. What are the important benefits of using Hibernate Framework?

 

Ans. Some of the important benefits of using hibernate framework are:

 

(i) Hibernate eliminates all the boiler-plate code that comes with JDBC and takes care of managing resources, so we can focus on business logic.

(ii) Hibernate framework provides support for XML as well as JPA annotations, that makes our code implementation independent.

(iii) Hibernate provides a powerful query language (HQL) that is similar to SQL. However, HQL is fully object-oriented and understands concepts like inheritance, polymorphism, and association.

(iv) Hibernate is an open source project from Red Hat Community and used worldwide. This makes it a better choice than others because learning curve is small and there are tons of online documentation and help is easily available in forums.

(v) Hibernate is easy to integrate with other Java EE frameworks, it’s so popular that Spring Framework provides built-in support for integrating hibernate with Spring applications.

(vi) Hibernate supports lazy initialization using proxy objects and perform actual database queries only when it’s required.

(vii) Hibernate cache helps us in getting better performance.

(viii) For database vendor specific feature, hibernate is suitable because we can also execute native sql queries.

(ix) Overall hibernate is the best choice in current market for ORM tool, it contains all the features that you will ever need in an ORM tool.

 

Q.3. Explain Hibernate architecture.

 

Ans. Hibernate has a layered architecture which helps the user to operate without having to know the underlying APIs. Hibernate makes use of the database and configuration data to provide persistence services (and persistent objects) to the application. It includes many objects such as persistent object, session factory, transaction factory, connection factory, session, transaction etc.

 

The Hibernate architecture is categorized in four layers.

(i) Java application layer

(ii) Hibernate framework layer

(iii) Backhand API layer

(iv) Database layer

 

 

Q.4. What are the advantages of Hibernate over JDBC?

 

Ans. Some of the important advantages of Hibernate framework over JDBC are:

 

(i) Hibernate removes a lot of boiler-plate code that comes with JDBC API, the code looks cleaner and readable.

(ii) Hibernate supports inheritance, associations, and collections. These features are not present with JDBC API.

(iii) Hibernate implicitly provides transaction management, in fact, most of the queries can’t be executed outside transaction. In JDBC API, we need to write code for transaction management using commit and rollback.

(iv) JDBC API throws SQLException that is a checked exception, so we need to write a lot of try-catch block code. Most of the times it’s redundant in every JDBC call and used for transaction management. Hibernate wraps JDBC exceptions and throw JDBCException or HibernateException un-checked exception, so we don’t need to write code to handle it. Hibernate built-in transaction management removes the usage of try-catch blocks.

(v) Hibernate Query Language (HQL) is more object-oriented and close to Java programming language. For JDBC, we need to write native SQL queries.

(vi) Hibernate supports caching that is better for performance, JDBC queries are not cached hence performance is low.

(vii) Hibernate provides option through which we can create database tables too, for JDBC tables must exist in the database.

(viii) Hibernate configuration helps us in using JDBC like connection as well as JNDI DataSource for the connection pool. This is a very important feature in enterprise application and completely missing in JDBC API.

(ix) Hibernate supports JPA annotations, so the code is independent of the implementation and easily replaceable with other ORM tools. JDBC code is very tightly coupled with the application.

 

Q.5. What are the differences between constructor injection and setter injection?

 

Ans.

No.

Constructor Injection

Setter Injection

1)

No Partial Injection

Partial Injection

2)

Doesn't override the setter property

Overrides the constructor property if both are defined.

3)

Creates a new instance if any modification occurs

Doesn't create a new instance if you change the property value

4)

Better for too many properties

Better for a few properties.

 

Top articles
5 DIY Photography Hacks to Capture Amazing Shots Published at:- Why Is Lavender Oil The maximum popular critical Oil Published at:- What is the natural effect of NFTs? Published at:- The Maldives is fostering the globe's first floating city Published at:- Oscar Winning Movies and Nominations list 2022 Published at:- Forecasting the Population of the United States in 2022: Trends and Insights Published at:- The Mental Health Benefits of Swimming Published at:- The Top 10 Amazon Prime Shows You Need to Watch Right Now Published at:- How many Instagram users are there in India? Published at:- Why Are Top Indian Wrestlers Protesting in Janttar Mattar Published at:- What Is US Inflation Data Published at:- Cannes Film Festival 2023 awardee’s list Published at:- Celebrating Navratri: A Divine Commemoration of Goddess Durga Published at:- How was the Ram temple in Ayodhya in the past and who built it Published at:- March 2024's Night Sky Delights: 6 Stunning Astronomical Events You Must Witness Published at:- Top Cost-Effective Wedding Venues in Mexico You'll Love: Saying "I Do" Without Breaking the Bank Published at:- Top 5 Celebrations around the World for World Heritage Day 2024 Published at:- 5 Heartfelt DIY Mother's Day Gift Ideas for 2024 Published at:- The Ultimate Guide: Buying Bitcoin in India Instantly with ZebPay Published at:- Beyond the Thermometer: Human Influence on the Heatwaves Published at:- Green Highway Signs: Evolution and Impact on Drivers Published at:- Hibernate – Java Interview Questions Published at:- Surviving the Storm: Expert Advice for Protecting Houseplants Published at:- NEET (MCQs on Plant Taxonomy) Published at:- Business, Economy & Banking (MCQs) Published at:- Important Days and Events (MCQs) Published at:- Short Guide for Parents on Infant Care Curriculum Published at:- Reports and Indices (MCQs) Published at:-

Hibernate – Java Interview Questions