Follow @devglan

hibernate Interview Questions and Answers

Each hibernate Interview Question Sets contains 12 questions.

1)   What is Hibernate?

Hibernate which is an implementation of JPA(Java Persistence API), is an ORM(Object Relational Mapping) tool which maps database tables with Java Objects and then provides various API?s to perform different types of operations on the data tables.

2)   Difference between JPA and Hibernate?

JPA is a specification for accessing , persisting and managing the data between java objects and the relational database. As the definition says, its API , it is only the specification. There is no implementation for the APIs.JPA is just the guidelines to implement the ORM and there is no underlying code for implementation.Whereas, Hibernate is the actual implementation of JPA guidelines.

3)   What is EntityManager?

EntityManager is an interface used to implement the persistence property to access entities in an application's persistent context.It provides the following 2 tpes of interfaces.
1.2. Application-managed Entitymanager

4)   What are the different approached to obtain entiryManager instance in Hibernate?

1. Using the container injection
2. Using the EntityManagerFactory interface
3. Looking up the EntityManager through JNDI.

5)   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:

Configuration config = new Configuration();
config.setProperties( System.getProperties() );
SessionFactory sessions = config.buildSessionFactory();

6)   What is the significance of insertable and updatable property in column definition in hibernate?

Insertable and updatable parameter control the behaviour of the persistent provider. If the insertable parameter is set to false, then the specified field or property is not included in the insert statement.
If the updatable parameter is set to false, then the field or property of an entity can't be updated in the DB.
@Column(name = "CUST_ID", insertable = "false", updatable = "false")

7)   What is the behaviour of @Enumerated annotation in hibernate?

@Enumerated indicates that the field's persistent property should be stored in the form of an Enumeration. The possible values of EnumType can be ORDINAL or STRING.
@Enumerated(EnumType.ORDINAL) - Ordinal returns its posiion in its enum declaration, lke 0, 1, 2
@Enumerated(EnumType.STRING) - String value of the enum is saved in the DB.

8)   What is the behaviour of @Temporal annotation in hibernate?

The DATE, TIME, TIMESTAMP types can be used to map a temporal type.
potected Date createdDate;

If no value is specified for the TemporalType parameter of the @Temporal annotations, the TIMESAMP i selected as default value.

9)   What?s HQL in Hibernate?

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. We write HQL based on our entity model classes and their relationship.

10)   What are the benefits of using Hibernate template?

Following are some key benefits of using Hibernate template:
1. Session closing is automated.
2. Interaction with hibernate session is simplified.
3. Exception handling is automated.

11)   How can we see hibernate generated SQL on console?

We need to add following in hibernate configuration file to enable viewing SQL on the console for debugging purposes:

12)   What the benefits are of hibernate over JDBC?

1.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.
2.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.
3.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.

Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.