Hibernate Interview Questions

  • 1
    What is Hibernate?

    Hibernate is one of the most popular Java frameworks that simplify the development of Java application to interact with the database. It is an Object-relational mapping tool. Hibernate also provides a reference implementation of Java API.It is referred as a framework which comes with an abstraction layer and also handles the implementations internally. The implementations include tasks like writing a query for CRUD operations or establishing a connection with the databases, etc.Hibernate develops persistence logic, which stores and processes the data for longer use. It is a lightweight tool and most importantly open-sourced which gives it an edge over other frameworks.

  • 2
    What are the major advantages of Hibernate Framework? 

    • It is open-sourced and lightweight.
    • Performance of Hibernate is very fast.
    • Helps in generating database independant queries.
    • Provides facilities to automatically create a table.
  • 3
    What is an ORM tool?

    It is basically a technique that maps the object that is stored in the database. An ORM tool helps in simplifying data creation, manipulation, and access. It internally uses the Java API to interact with the databases.

  • 4
    What are the technologies that are supported by Hibernate?

    Hibernate supports a variety of technologies, like:

    • XDoclet Spring
    • Maven
    • Eclipse Plug-ins
    • J2EE
  • 5
    What is HQL?

    HQL is the acronym of Hibernate Query Language. It is an Object-Oriented Query Language and is independent of the database.

  • 6
    Name some of the important interfaces of Hibernate framework?

    Hibernate interfaces are:

    • SessionFactory (org.hibernate.SessionFactory)
    • Session (org.hibernate.Session)
    • Transaction (org.hibernate.Transaction)
  • 7
    What is One-to-Many association in Hibernate?

    In this type of association, one object can be associated with multiple/different objects. Talking about the mapping, the One-to-Many mapping is implemented using a Set Java collection that does not have any redundant element. This One-to-Many element of the set indicates the relation of one object to multiple objects.

  • 8
    How to integrate Hibernate and Spring?

    Spring is also one of the most commonly used Java frameworks in the market today. Spring is a JavaEE Framework and Hibernate is the most popular ORM framework. This is why Spring Hibernate combination is used in a lot of enterprise applications. 

  • 9
    What do you mean by Hibernate Configuration File?

    Hibernate Configuration File mainly contains database-specific configurations and are used to initialize SessionFactory. Some important parts of the Hibernate Configuration File are Dialect information, so that hibernate knows the database type and mapping file or class details.

  • 10
     What is Hibernate SessionFactory?

    SessionFactory is the factory class that is used to get the Session objects. The SessionFactory is a heavyweight object so usually, it is created during application startup and kept for later use. This SessionFactory is a thread-safe object which is used by all the threads of an application. If you are using multiple databases then you would have to create multiple SessionFactory objects.

  • 11
    What are the key components of a Hibernate configuration object?

    The configuration provides 2 key components, namely:

    • Database Connection: This is handled by one or more configuration files.
    • Class Mapping setup: It helps in creating the connection between Java classes and database tables.
  • 12
    What are the collection types in Hibernate?

    There are five collection types in hibernate used for one-to-many relationship mappings.

    • Bag
    • Set
    • List
    • Array
    • Map
  • 13
     What are the benefits of using Hibernate template?

    The following are the benefits of using this Hibernate template class:

    • Automated Session closing ability.
    • The interaction with the Hibernate Session is simplified.
    • Exception handling is automated.
  • 14
    What is Dirty Checking in Hibernate?

    Hibernate incorporates Dirty Checking feature that permits developers and users to avoid time-consuming write actions. This Dirty Checking feature changes or updates fields that need to be changed or updated, while keeping the remaining fields untouched and unchanged.

  • 15
     How can you share your views on mapping description files?

    • Mapping description files are used by the Hibernate to configure functions.
    • These files have the *.hbm extension, which facilitates the mapping between database tables and Java class.
    • Whether to use mapping description files or not this entirely depends on business entities.
  • 16
    What is meant by Light Object Mapping?

    The means that the syntax is hidden from the business logic using specific design patterns. This is one of the valuable levels of ORM quality and this Light Object Mapping approach can be successful in case of applications where there are very fewer entities, or for applications having data models that are metadata-driven.

  • 17
    What is ORM?

    ORM is an acronym for Object/Relational mapping. It is a programming strategy to map object with the data stored in the database. It simplifies data creation, data manipulation, and data access.

  • 18
    Mention some of the advantages of using ORM over JDBC.

    ORM has the following advantages over JDBC:

    • Application development is fast.
    • Management of transaction.
    • Generates key automatically.
    • Details of SQL queries are hidden.
  • 19
    Define criteria in terms of Hibernate.

    The objects of criteria are used for the creation and execution of the object-oriented criteria queries.

  • 20
     List the key components of Hibernate.

    Key components of Hibernate are:

    • Configuration
    • Session
    • SessionFactory
    • Criteria
    • Query
    • Transaction
  • 21
    Mention two components of Hibernate configuration object.

    • Database Connection
    • Class Mapping Setup
  • 22
    How is SQL query created in Hibernate?

    The SQL query is created with the help of the following syntax:

    • Session.createSQLQuery
  • 23
    What is lazy loading in hibernate?

    • Lazy loading in hibernate improves the performance. It loads the child objects on demand.
    • Since Hibernate 3, lazy loading is enabled by default, and you don’t need to do lazy=”true”. It means not to load the child objects when the parent is loaded.
  • 24
     Is it possible to perform collection mapping with One-to-One and Many-to-One?

    No, collection mapping can only be performed with One-to-Many and Many-to-Many.

  • 25
    How many types of association mapping are possible in hibernate?

    There can be 4 types of association mapping in hibernate.

    1. One to One
    2. One to Many
    3. Many to One
    4. Many to Many
  • 26
    How to make an immutable class in hibernate?

    If you mark a class as mutable=”false”, the class will be treated as an immutable class. By default, it is mutable=”true”.

  • 27
    What is hibernate mapping file?

    Hibernate mapping file is used to define the entity bean fields and database table column mappings. We know that JPA annotations can be used for mapping but sometimes XML mapping file comes handy when we are using third party classes and we can’t use annotations.

  • 28
    Hibernate SessionFactory is thread safe?

    Internal state of SessionFactory is immutable, so it’s thread safe. Multiple threads can access it simultaneously to get Session instances.

  • 29
    Hibernate Session is thread safe?

    Hibernate Session object is not thread safe, every thread should get it’s own session instance and close it after it’s work is finished.

  • 30
    What is use of Hibernate Session merge() call?

    Hibernate merge can be used to update existing values, however this method create a copy from the passed entity object and return it. The returned object is part of persistent context and tracked for any changes, passed object is not tracked. 

  • 31
    What will happen if we don’t have no-args constructor in Entity bean?

    Hibernate uses Reflection API to create instance of Entity beans, usually when you call get() or load() methods. The method Class.newInstance() is used for this and it requires no-args constructor. So if you won’t have no-args constructor in entity beans, hibernate will fail to instantiate it and you will get HibernateException.

  • 32
    What are the collection types in Hibernate?

    There are five collection types in hibernate used for one-to-many relationship mappings.

    • Bag
    • Set
    • List
    • Array
    • Map
  • 33
    Why we should not make Entity Class final?

    Hibernate use proxy classes for lazy loading of data, only when it’s needed. This is done by extending the entity bean, if the entity bean will be final then lazy loading will not be possible, hence low performance.

  • 34
    What is the benefit of native sql query support in hibernate?

    Native SQL Query comes handy when we want to execute database specific queries that are not supported by Hibernate API such as query hints or the CONNECT keyword in Oracle Database.

  • 35
    What is Named SQL Query?

    • Hibernate provides Named Query that we can define at a central location and use them anywhere in the code. We can created named queries for both HQL and Native SQL.
    • Hibernate Named Queries can be defined in Hibernate mapping files or through the use of JPA annotations @NamedQuery and @NamedNativeQuery.
  • 36
     What is Hibernate Proxy and how it helps in lazy loading?

    Hibernate uses proxy object to support lazy loading. Basically when you load data from tables, hibernate doesn’t load all the mapped objects. As soon as you reference a child or lookup object via getter methods, if the linked entity is not in the session cache, then the proxy code will go to the database and load the linked object. It uses javassist to effectively and dynamically generate sub-classed implementations of your entity objects.

  • 37
    What is the benefit of Hibernate Tools Eclipse plugin?

    Hibernate Tools plugin helps us in writing hibernate configuration and mapping files easily. The major benefit is the content assist to help us with properties or xml tags to use. It also validates them against the Hibernate DTD files, so we know any mistakes before hand. 

  • 38
    What inheritance mapping strategies are available in Hibernate?

    Hibernate have 3 ways of inheritance mapping, They are

    • Table per hierarchy
    • Table per concrete class
    • Table per subclass
  • 39
    Which annotation is used to declare a class as a hibernate bean ?

    @Entity annotation is used to declare a class as an entity.

  • 40
    Explain what is a dialect?

    Hibernate Dialect is used to specify the type of database we are going to use. Hibernate requires this to know in advance so it is able to generate appropriate type of SQL statements based on database type.

  • 41
    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.

  • 42
    Explain the persistent classes in Hibernate?

    In hibernate, the Java classes whose instances and objects are stored in database classes are called persistent classes

  • 43
    What is Lazy loading in hibernate ?

    It is a technique in where the objects are loaded on the requirement basis. Since the Hibernate 3 version, the lazy loading is by default enabled so that the child objects are not loaded while the parent is loaded.

  • 44
    What are concurrency strategies?

    The concurrency strategies are the mediators who are responsible for storing items and retrieving them from the cache.In case of enabling a second level cache, the developer must decide for each persistent class and collection, and also which cache concurrency, has to be implemented.

  • 45
    What is Query level cache in Hibernate?

    In hibernate, a cache query can be implemented that results in sets and integrates closely with the second level cache.It is an optional feature and it requires two additional cache regions that can hold the cached query results and also the timestamps whenever a table is updated. This is useful only for the queries that run frequently holding the same parameters

  • 46
    What is second level cache in Hibernate?

    It is an optional cache. And, always the first level cache will be consulted before any attempt is performed to locate an object in the second level cache. This cache can be configured on a pre-collection and per-class basis and it is mainly responsible for caching objects across the sessions

  • 47
    What is first level cache in Hibernate?

    It is session cache and mandatory cache. It is from first level cache through which all the requests must pass.The session object stores an object under its control before committing it to the database.

  • 48
    What are persistent classes in hibernate?

    Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate.

  • 49
    Where Object/relational mappings are defined in hibernate?

    An Object/relational mappings are usually defined in an XML document. This mapping file instructs Hibernate how to map the defined class or classes to the database tables. We should save the mapping document in a file with the format <classname>.hbm.xml.

  • 50
    What is many-to-one association?

    • A many-to-one association is the most common kind of association where an Object can be associated with multiple objects. For example a same address object can be associated with multiple employee objects.
    • <many-to-one> element is used to define many-to-one association. The name attribute is set to the defined variable in the parent class. The column attribute is used to set the column name in the parent table.