loading...

Fresher Necessary 10 Hibernate Interview Question

onlineinterview profile image Mayank K. Chaurasiya Updated on ・5 min read

Hibernate Interview Question

1. What is Hibernate?

  • Hibernate is a free software, which is distributed under GNU Lesser General public license 2.1.
  • It is categorized under the Object Relational Mapping (ORM), which features the mapping of Java classes to data tables and mapping from Java data types to SQL data types.
  • It is written in Java and is JVM (Java Virtual Machine) platform based.
  • Hibernate provides the data query and retrieval facilities.
  • It is one of the most widely used ORM tools for the Java applications.
  • Hibernate is highly considered to be used in the enterprise applications for database operations.

2. Mention the Key components of Hibernate?

The Key components of Hibernate are:

Session: It is used to get a physical network with a database.
Transaction: It represents the unit of work with a database.
Query: It uses SQL and HQL string to retrieve the data from the database and create objects.
Criteria: It is used create and execute object-oriented queries and retrieve the objects.
Configuration: It represents the properties of files required by Hibernate
Session Factory: It configures hibernate for the application using the provided configuration file and instantiates the session object.

3. Which design patterns are used in Hibernate?

Hibernate uses following Design patterns

  • Domain Model Pattern – An object model of the domain that incorporates both behavior and data.
  • Proxy Pattern for lazy loading.
  • Unit of Work (as part of Session object)
  • Factory Pattern in Session Factory
  • Query Object for Criterion API
  • Data Mapper – A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself.
  • Active Record Pattern
  • DAO
  • Object-Relational Mapping(ORM)

4.List Data types supported in Hibernate?

Hibernate maps Java data types into databases. Java type can be mapped into one or more columns in the database. We can set Object's record as primitive types like int, byte or more complex, for example, java.util.Currency.

Data types supported in Hibernate are
Primitive types.
Date and time types.
Binary and large object types.
Other JDK-related types.

5. What are the different ORM levels in Hibernate?

There are basically four different ORM levels in Hibernate:

Pure Relational ORM: At this level entire application is designed around the relational model. All the operations are SQL based at this level.
Light Object Mapping: At this level entity classes are mapped manually to relational tables. Business logic code is hidden from the data access code. Applications with less number of entities use this level.
Medium Object Mapping: In this case, the application is designed around an object model. Most of the SQL code is generated at compile time. Associations between objects are supported by the persistence mechanism. The object-oriented expression language is used to specify queries.
Full Object Mapping: This is one of the most sophisticated objects modeling level. It supports composition, inheritance, polymorphism, and persistence. The persistent classes do not inherit any special base class at this level. There are efficient fetching and caching strategies implemented transparently to the application.

6. What is @Transient in Hibernate? What is use of this?

Hibernate supports various object states. One of this object state is transient. In hibernate which is object relation mapping it will map or create every variable with the class we have defined. For example if we declare a new variable it will add its entry in database. But if a variable is marked @Transient then it will not be added in ---

6. What is @Transient in Hibernate? What is use of this?

Hibernate supports various object states. One of this object state is transient. In hibernate which is object relation mapping it will map or create every variable with the class we have defined. For example if we declare a new variable it will add its entry in database. But if a variable is marked @Transient then it will not be added in the database. This means it has no persistence representation in that hibernate session. And after an application is closed these transient objects will be destroyed through garbage collection.

class Edufect{

    private int id;

    private String courseName;

    @Transient

    private int courseId;

}

So in this in our database we can see the entry of id column, courseName but no courseId column. But if we remove transient annotation from courseId then it will be added as an int to the database.

7. What are the key differences between SQL Query and Hibernate Query Language (HQL)?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries which in turns perform action on database.

Few differences between SQL and HQL.

  • SQL is based on relational database model whereas HQL is a combination of OOPS with Relational database concept.
  • SQL manipulates data stored in tables and modifies its rows and columns while HQL is concerned about objects and its properties.
  • SQL is concerned about the relationship that exists between two tables while HQL is concerned about relation between objects.

8. What is Hibernate caching?

Hibernate caches Query data and makes the application run faster.

If used correctly, the hibernate cache can be very useful in achieving the faster application running performance.

The main idea lying behind the cache is reducing the number of database queries, which results in reduced throughput time of the application.

9. Describe the important interfaces of Hibernate framework?

Important interfaces of Hibernate framework are:

  • SessionFactory (org.hibernate.SessionFactory)
  • It is an immutable thread safe cache of compiled mappings for a single database. We are supposed to initialize SessionFactory once and then we are allowed to cache and reuse it. The SessionFactory instance is used to return the session objects for database operations.
  • Session (org.hibernate.Session)
  • It is a single threaded and short-lived object, which represents a conversation between the persistent store and the application. The session should be opened only when it is required and should be closed as soon as the user is done. The session object is the interface between hibernate framework and Java application code and it provides methods for the CRUD operations.
  • Transaction (org.hibernate.transaction)
  • It is a single threaded and short-lived object used by the application, which specifies atomic units of work. The application is abstracted from the underlying JDBC or JTA transaction.

10. Describe the method used to create an HQL Query and SQL Query?

Session.createQuery is used to create a new instance of a query for the HQL query string.

Session.createSQLQuery is used to create a new instance of a query for the SQL query string.

Thanks for reading.

References

Hibernate Interview Questions
Common Hibernate Interview Questions
Frequently Asked Hibernate Interview Questions

Posted on Nov 1 '18 by:

onlineinterview profile

Mayank K. Chaurasiya

@onlineinterview

Hi, I am Mayank writing tutorials in an online interview question.

Discussion

markdown guide
 

Thank you so much for helping us, your blog helps a lot.