Friday 27 April 2018

Hibernate Interview Questions

1. What’s Hibernate?

Hibernate is a popular framework of Java which allows an efficient Object Relational mapping using configuration files in XML format. After java objects mapping to database tables, database is used and handled using Java objects without writing complex database queries.

2. What is ORM?

ORM (Object Relational Mapping) is the fundamental concept of Hibernate framework which maps database tables with Java Objects and then provides various API’s to perform different types of operations on the data tables.

3. How properties of a class are mapped to the columns of a database table in Hibernate?

Mappings between class properties and table columns are specified in XML file as in the below example:

4. What’s the usage of Configuration Interface in hibernate?

Configuration interface of hibernate framework is used to configure hibernate. It’s also used to bootstrap hibernate. Mapping documents of hibernate are located using this interface.

5. How can we use new custom interfaces to enhance functionality of built-in interfaces of hibernate?

We can use extension interfaces in order to add any required functionality which isn’t supported by built-in interfaces.

6. Should all the mapping files of hibernate have .hbm.xml extension to work properly?

No, having .hbm.xml extension is a convention and not a requirement for hibernate mapping file names. We can have any extension for these mapping files.

7. How do we create session factory in hibernate?

To create a session factory in hibernate, an object of configuration is created first which refers to the path of configuration file and then for that configuration, session factory is created as given in the example below:
Java
1
2
3
4
Configuration config = new Configuration();
config.addResource("myinstance/configuration.hbm.xml");
config.setProperties( System.getProperties() );
SessionFactory sessions = config.buildSessionFactory();

8. What are POJOs and what’s their significance?


POJOs( Plain Old Java Objects) are java beans with proper getter and setter methods for each and every properties.
Use of POJOs instead of simple java classes results in an efficient and well constructed code.

9. What’s HQL?

HQL is the query language used in Hibernate which is an extension of SQL. HQL is very efficient, simple and flexible query language to do various type of operations on relational database without writing complex database queries.

10. How can we invoke stored procedures in hibernate?

In hibernate we can execute stored procedures using code as below:

11. What is criteria API?

Criteria is a simple yet powerful API of hibernate which is used to retrieve entities through criteria object composition.

12. What are the benefits of using Hibernate template?

Following are some key benefits of using Hibernate template:
a. Session closing is automated.
b. Interaction with hibernate session is simplified.
c. Exception handling is automated.

13. What are the two types of collections in hibernate?

Following are the two types of collections in hibernate:
a. Sorted Collection
b. Order Collection

14. What’s the difference between session.save() and session.saveOrUpdate() methods in hibernate?

Sessionsave() method saves a record only if it’s unique with respect to its primary key and will fail to insert if primary key already exists in the table.
saveOrUpdate() method inserts a new record if primary key is unique and will update an existing record if primary key exists in the table already.

15. What the benefits are of hibernate over JDBC?

a. Hibernate can be used seamlessly with any type of database as its database independent while in case of JDBC, developer has to write database specific queries.
b. Using hibernate, developer doesn’t need to be an expert of writing complex queries as HQL simplifies query writing process while in case of JDBC, its job of developer to write and tune queries.
c. In case of hibernate, there is no need to create connection pools as hibernate does all connection handling automatically while in case of JDBC, connection pools need to be created.

More about Hibernate:

1 comment:

  1. I am a first visitor to your blog, you have shared some hibernate interview questions. This is really helpful to me, I have bookmarked this page for my reference. Share more like this.
    Hibernate Training in Chennai | JAVA Course in Chennai

    ReplyDelete