Hibernate Interview Questions and Answer 2020

  • By
  • December 28, 2019
  • Full Stack
Hibernate Interview Questions and Answer 2020

Hello guys, in this blog of Full Stack Training in Pune, I will discuss frequently asked Hibernate interview questions –

1>What is Hibernate Framework?

Answer : Hibernate is one of Java’s popular framework for Persistency. Hibernate is an open-source framework. It is used to simplify the Java Application code for database interaction. Hibernate is ORM mapping and Object-Oriented tool. Hibernate provides an abstraction layer which deals with internal implementation of Java API this implementation is taken care about provide CRUD operation, establishing a connection between database, form a query, etc. Hibernate is a lightweight tool it can easily integrate with other frameworks like Spring, Struts framework.

2>What is ORM Tool?

Basically ORM tool is use to store and retrieve objects from relational database for that it provides a API.ORM technique which allows an application object deals with information like objects, class rather than rows ,columns and tables by using Object Relational mapping.

chain jh

3> What is advantage of Hibernate framework over JDBC?

Answer : Hibernate overcome the problem of JDBC .please go through below points for same

1>Hibernate overcome data dependency problem which is commonly face in JDBC.

2>When we change the databases then it cost a lot while working with JDBC, Hibernate gives s solution for this problem.

3>Code portability is not available with working on JDBC. These problems easy solution is given by hibernate.

4>Hibernate is based on ORM . Java Support inheritance, association and polymorphism but for data persistency these features are not support by JDBC

5>In JDBC exception-handling is mandatory while hibernate overcome that part as well.

6>Being a framework hibernate reduces the code length . While working with hibernate a lot of boilerplate code is need.

 

4>what is Hibernate configuration file?

Ans : Hibernate is an ORM tool it provides automated persistence for that Hibernate needs Hibernate configuration file which contains database-specific configuration which is used to initialized SessionFactory.A hibernate configuration file is used to map Hibernate Properties.

5>Explain Hibernate  Configuration properties?

  The following Hibernate properties is used in the configuration file.

  • hibernate dialect: This property ensures hibernate to generate appropriate SQL for the chosen database.
  • hibernate.connection.driver_class : JDBC driver class name for making the connection with a database
  • hibernate.connection.url:JDBC URL for creating a database instance
  • hibernate.connection.username:Database username
  • hibernate.connection.password:Database password
  • hibernate.connection.pool_size: In the hibernate connection pool mention, the number of connections waiting.
  • hibernate.connection.autocommit:It allows an auto-commit mode for JDBC connection

If hibernate is connect use along with application server then following properties are used for JNDI

  • hibernate.connection.datasource: JNDI name for application server context which is used in the application.
  • hibernate.JNDI.class: JDNI class for InitialContext
  • hibernate.jndi.url: provides JNDI Url for JNDI mapping

5> Name some of the important interfaces of the Hibernate Framework?

Important Hibernate framework interfaces are:

  • SessionFactory (org.hibernate.SessionFactory) :
  • Session(org.hibernate.Session)
  • Transaction(org.hibernate.Transaction)

6> What is the hibernate mapping file?

Ans: Hibernate mapping file is used to map entity bean along with the database. In Hibernate JPA annotations are used to map entity beans but if we want to map third party classes the Hibernate mapping file is the preferable solution.

 

7> What is the important Annotation is used for Hibernate Mapping?

Ans : Hibernate supports JPA annotations and it has some other annotations in org.hibernate.annotations package. Important  JPA annotations are as follows.

  • javax.persistence.Entity: It is used in model classes to specify the entity beans.
  • javax.persistence.Table: It is used in model classes to specify the entity beans to define the corresponding table name in database.
  • javax.persistence.Access: It is used in model the access type, either field or property. javax.persistence.Id: It is used in model class to define primary key.
  • javax.persistence.EmbeddedId: It is used to define composite primary key in the entity bean.
  • javax.persistence.Column: It is use to define the column name in database table.
  • javax.persistence.GeneratedValue: It is used to define the strategy to be used for generation of primary key It is used in combination with javax.persistence.GenerationType enum.
  • javax.persistence.OneToOne: It is used to define the one-to-one mapping between two entity beans. Similar annotations is OneToMany, ManyToOne and ManyToMany
  • org.hibernate.annotations.Cascade: It is used to define the cascading between two entity beans, used with mappings. It works in combination with org.hibernate.annotations.CascadeType
  • javax.persistence.PrimaryKeyJoinColumn: It is used to define the property for foreign key. It is used with org.hibernate.annotations.GenericGenerator and org.hibernate.annotations.Parameter

 

 

8>What is Hibernate Session Factory and how to configure it?

Session Factory is an interface that is used to get Session objects. SessionFacory is a heavyweight object. It is created one per application.SessionFactory is read all the information from the hibernate configuration file and creates a connection object to the database and gives the session object. Normally an application has a single instance of Session Factory and threads are used to serve client requests.

The session factory is immutable. Once it created it can’t change its internal state. Session Factory includes Object /Relational Mapping metadata.

9>What is difference between openSession and getCurrentSession?

Hibernate has provide  getCurrentSession() method of Session Factory. This method returns the session object which is bound to the hibernate context. To achieve this Full Stack Classes in Pune need to add the following mapping in hibernate.cfg.file as belong to the hibernate context. The session that obtain by this method is no need to close. Once we close the SessionFactory object it will close the session object.

<property name=”hibernate.current_session_context_class”>thread</property>

Hibernate SessionFactory openSession() method is gives new session everytime.Once all database operation is finish Full Stack Course in Pune should close session. For every request in multithreaded environment it will open new session.

10>What is difference between Hibernate Session get() and load() method?

In hibernate framework there are two ways to fetch data from database   get() and load()

Both methods does same job but here is few differences.

                                  get()                                    load()
 

In get() method we can access data immediately when it is called.

 

When we call load () it will return proxy object to load data when actually it is needed this approach is called as lazy loading. It is better approach.

 

We should use get() when we sure that there is data in the database. load() throw exception if data is not found so we can use this if we know data is present. hence we should call this only we know data that data is present.

 

 

11>What is difference between Hibernate save(), saveOrUpdate() and persist() methods?

Hibernate is ORM tool used to save entity in the database. Hibernate session save () is invoke without transaction in case .In case of multiple mapping entries the primary object get save into database this leads to data inconsistencies issue. In addition ,save() returns generated id immediately.

Hibernate persist() method is also use to save data in database but this method is use with in a transaction only hence all  object mappings are preserved. This method don’t not return generated id .In this method data persist happen when it is needed.

Hibernate saveorUpdate() is used for insert or update queries  depends on provided data. If data is present in database it will update query is called. Even we can use saveOrUpdate() without transaction object.

12>What is HQL ?

Ans :HQL is stands for Hibernate Query Language it an Object Oriented  Query Language .

It is similar to SQL (Structure Query Language) but it works on classes, Object and properties rather than  table and row. It support polymorphic queries .In hibernate Query Interface is use to execute queries.

13>What are the different state of persistent entity ?

Ans :In hibernate framework entities can be in three different state such as transient, persistent ,detached.

Transient state: When any object is created by using new keyword and not associated with hibernate framework  then it in Transient state.

Persistent state: When an object is associated session Object by calling save() or persist() then that Object is in Persistent state.

Detached state :When we called close() method on  session object then Object is in detached state.

14>How to can see generated SQL on console in hibernate?

We can see generated SQL in console by adding following property in hibernate configuration file we need add following  property  for debugging purpose.

<property name=”show_sql”>true</property>

15> What is lazy loading in hibernate?

In Hibernate improves the performance by using Lazy loading .In Hibernate 3 lazy loading is enabled by default. Basically it loads child objects on demand basis that means when parent is loading no need to load child object.

16> Explain hibernate architecture?

Hibernate architecture contains many interfaces such as Transaction, Configuration, SessionFactory ,Session etc.

Hibernate architecture has following four layer

1>Java API Layer

2>Hibernate framework Layer(API)

3>Persistent layer (backend API)

4>Database layer.

 

hibernate

Hibernate elements are :

Hibernate SessionFactory is an interface in hibernate API .SessionFactory provides a method to get session object.SessionFactory:

Session

            Session interface provides methods to insert,update and delete Object from database . It also provides transaction and Query and Criteria object.

Transaction

            In Hibernate Transaction management is implement through Transation .It provides unit of work .

17>How HQL query is created?

Answer:

The query object is obtained through the session object

            Session.createQuery()

18>How Inheritance is mapping in Hibernate?

Ans: Inheritance is mapped using the following strategy

1>Table per concrete class

2>Table per hierarchy

3>Table per subclass.

19>what is cascading in hibernate?

Answer:

When in entity relation one entity depends on other e.g. Employee and department. Without the Department, the Employee doesn’t have any meaning. When we delete Department the Employee entity must be deleted this we can do using cascading.

20> Does hibernate support polymorphism?
Ans :Yes ,Hibernate support polymorphism. Polymorphism queries and association is supported through mapping strategies.

Author:

Jayshree Chaudhary

FullStack Trainer

                                                                                 

For Free Demo classes Call: 8237077325
Registration Link:Click Here!

Call the Trainer and Book your free demo class now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*