Hibernate Interview Questions

Hibernate Interview Questions | Freshers & Experienced

  • Calvin
  • 02nd Dec, 2019
  • 794 Followers

Hibernate Interview Questions

Q1. What is difference between Getcurrentsession() and Opensession() method in hibernate?

Q2. What is open session in hibernate?

An open session method in hibernate is used to open a new session. We have to close the session object as soon as we are done with all the operations of the database. It is necessary to open a new session for each request in the multi-threaded environment. Sessionfactory.opensession() is used in this regard. We have to explicitly flush and close the unused session objects. We need to create one session object per request in a multithreaded environment and only one session per request in the applications of the web because session objects are not considered to be thread-safe. In hibernation, when an open session is applied, it returns a new session object on every call.

Q3. What is the default fetch type in hibernate?

The default fetch type in the hibernate is the lazy fetching. Hibernate uses lazy select fetching for collections. It also uses the lazy proxy fetching for single-valued associations.

Q4. Explain dirty checking in hibernate?

Hibernation offers a dirty checking facility. It is advantageous to save the time and effort of developers in order to update the database when states of objects are changed or modified inside a transaction. Hibernate recognizes the changes in object states and thereby synchronized with the database for its updating. Here the essential updating and modifications are done without affecting the other fields in the database. We can say it is a method of checking all managed entities (each persistent object of that entity) properties.

Q5. What is Evict in hibernate?

Evict() method in hibernate is used to detach/dissociate the object from its cache session as we need to make changes. Any change to the object will not be persistent, after detaching the object from the session. Suppose we are waiting for getting the input from the user, in this case, we can detach the object instance from the hibernate session and this is done by evict() method. In layman's language, we can say that it removes the specified object from the current session. One should also keep a check that the object is in session before calling the evict(). It occurs outside any transaction and evicts all the entries from the cache.

Q6. What are JPA Annotation?

JPA – Java Persistence API annotations are simplified as an object-relational mapping that allows defining how to map Java objects to relational database tables. They are mapped in a standard and portable way.

With a Java source code, a form of metadata like Java annotation is added. Since Java annotation is added with the source file, it can only be viewed in the source file.

Below are few JPA Annotations used for mapping java objects to the database table.

  • Javax.persistance.Entity
  • @Table
  • @Column
  • @Id
  • @GeneratedValue
  • @Version
  • @OrderBy
  • @Transient
  • @Lob

Q7. How can we invoke stored procedures in hibernate?

There are three ways to call stored procedures in the Hibernate.

  1. They are the NativeSQL,
  2. NamedNativeQuery,
  3. SQL-query in the XML mapping file.

In the NativeSQL, you can use createSQLQuery() to call a store procedure directly.

In the NamedNativeQuery, you can declare your store procedure inside the @NamedNativeQueries annotation.

In the SQL-query in the XML mapping file, you can declare your store procedure inside the “SQL-query” tag.

Q8. What is difference between session.get() and session.load() method in hibernate?

Q9. List few disadvantages of hibernate?

There is a certain disadvantage in the hibernate framework. Some of them we have listed below

  • It is not that easy to learn. Because there are lots of API to learn here.
  • Sometimes here the removal of errors and improving the efficiency of the system performance becomes slow and also difficult to deal with.
  • Those small projects having just a few tables it is not a good idea here to use this framework.
  • In hibernate, the developer has to just write every query separately in order to insert the objects as here you can insert multiple objects into the same query.

Q10. What is a proxy in hibernate?

Proxy is a function that is authorized to act as the deputy or substitute for another. In the Hibernate, the proxies are the classes that are generated dynamically by the Hibernate to help with the lazy loading. If you generate a class, then the Hibernate will generate a proxy class that extends the desired class. This subclass is implemented at the runtime. The proxy is created instead of querying the database directly, and this proxy will load the real object from the database whenever one of its methods is called.

Q11. Explain how Caching works in Hibernate?

Caching is a mechanism that is used to enhance the performance of the system. The cache memory lies between the application and the database and stores recently used data items. It is done to reduce the number of database hits as much as possible.

Hibernate uses multi-level caching such as first-level cache, second-level cache, and query-level cache. The first-level cache is the session cache which is mandatory through which all the requests must pass. The Second-level cache is optional which is consulted after an attempt is made to locate an object in the first-level cache. The query-level cache is implemented to cache for query resultsets. This is closely integrated with the second-level cache.

Q12. What is use of Hibernate QBC API?

Hibernate query by criteria (QBC) API is generally used to generate queries by use of criteria objects at run time. In Hibernate QBC API, you can apply rules like filtration and logical conditions.

Q13. What is Light Object Mapping in Hibernate?

In Light Object Mapping terminology, the mapping is done manually to the relational tables, where the entities are represented as classes. Using different design patterns, the code is hidden from the logic. This terminology is well known because it is used for applications with a smaller number of entities.

Q14. What is ordered collection and sorted collection in Hibernate?

The sorted collection is sorted using the sorting features provided by the Java collections framework. It is done in the memory of JVM that is running in hibernate. This sorting is done soon after reading data from the database using the Java Comparator. In the sorted collection, less the collection, the more efficient it will be the sorting.

The Ordered collection is used to sort the collection by using the order by clause for the results fetched. In the ordered collection, the more the collection, the more efficient will be the sorting.

Q15. What is Hibernate Framework?

Hibernate framework helps and also makes it easy in the development of the Java application. It is basically an object-relational mapping tool that also can be said those tools with object-oriented programming.

Below are the certain advantages of using this framework:

  • The performance here can be really expected faster as it already has a cache enable it by default.
  • It also allows creating tables and that too automatically in the database.

Q16. What is Java Persistence API (JPA)?

The Java Persistence API (JPA) is the specification of Java that is used to persist the data between the Java object and the relational database. So it essentially acts as a bridge between the object-oriented domain models and the relational database systems. The JPA is just a specification so it doesn’t perform any operation by itself. But the JPA specifications are implemented by ORM tools like the Hibernate, TopLink, and iBatis.

Q17. What are the important benefits of using Hibernate Framework?

         

Q18. Name some important interfaces of Hibernate framework?

           

Q19. What is hibernate configuration file?

The Hibernate configuration file has configuration parameters such as the mapping information that provides different functionalities to the Java class. This database-related mapping information is usually found in the configuration file. It is an XML file like hibernate.cfg.xml.

Q20. What is hibernate mapping file?

The Hibernate mapping file is used by the Hibernate framework to get the information regarding the mapping of the POJO class and the database table. The mapping information found in the hibernate mapping file is the mapping information of a POJO class name to the database table name and the mapping information of POJO class properties to the database table columns.

Q21. What is Hibernate SessionFactory and how to configure it?

                

Q22. What is Hibernate Session and how to get it?

                    

Q23. What is the difference between Hibernate Session get() and load() method?

               

Q24. What is hibernate caching? Explain Hibernate first level cache?

When we need to reduce the database queries, we take the advantage of using Cache. So automatically the throughput time of the application will be reduced. So hibernate cache is used for fast application performance.

There is a direct link with the session object and Hibernate first level cache. When the program runs, hibernate first level cache enables automatically and the option for disabling the cache is minimal. Either the object can be deleted or clear the cache thoroughly.

When a session is closed, all the caches will be closed automatically.

Q25. What is difference between Hibernate save(), saveOrUpdate() and persist() methods?

               

Q26. What is the difference between sorted collection and ordered collection, which one is better?

                  

Q27. What are the collection types in Hibernate?

The different collection types in the Hibernate are the ArrayType, BagType, CustomCollecctionType, IdentifierBagType, ListType, MapType, and the SetType.

Q28. What is HQL and what are it benefits?

HQL – Hibernate query language is hibernate's own language which is used to activate bulk operations in hibernate programs.

Below are the benefits of using HQL commands.

  • HQL commands are convenient to write any programs which will be able to execute in all types of databases. No further changes are required if we use HQL commands.
  • Object-oriented features also are supported by HQL

Q29. What is Query Cache in Hibernate?

The Query Cache in the Hibernate is used for caching the results of the queries. It is done so that the traffic between the application and the database can be reduced by storing the frequently accessed query results in the query cache. Query Cache is one of the three levels of caching, the other two being the first level cache and the second-level cache.

Q30. What is Named SQL Query?

The Hibernate Named query is used to query the database by some meaningful name. This gives the query an alias name so the application programmer needs not to scatter queries to all the java code.

Two ways of defining a named query in Hibernate is by annotation and by mapping file.

Q31. How to log hibernate generated SQL queries in log files?

             

Q32. What is Hibernate Proxy and how it helps in lazy loading?

For every entity, a proxy gets generated by the hibernate.

Proxy is generally generated during runtime and lazy loading is one of its behavior. To make it simple, let us talk about parent object and child object. When the parent object is under loading, the hibernate proxy decides to load the child object slow. Slow in loading is termed as lazy loading and it helps to keep the parent object as fast as possible.

Q33. How to implement relationships in hibernate?

            

Q34. What is cascading and what are different types of cascading?

                

Q35. What is HibernateTemplate class?

HibernateTemplate class is a helper class in Hibernate that is used to simplify the data access code. This class supports automatic conversion of HibernateExceptions checked exception into DataAccessExceptions which is an unchecked exception. Hibernate template classes are mainly used in business logic services.

Below are a few benefits of Hibernate template class.

  • The hibernate session’s interactions are simplified
  • The functions that are considered as common are simplified
  • The sessions while running get closed automatically.
  • The exceptions get caught easily and help to convert the exceptions to runtime exceptions.

Q36. How to integrate Hibernate with Servlet or Struts2 web applications?

              

Q37. Which design patterns are used in Hibernate framework?

                

Q38. What is Hibernate Validator Framework?

The Hibernate Validator framework allows you to express and validate the application constraints. It is not tied to a specific application tier or programming model, but it is available for both the server and client application programming. You need to add the Hibernate-validator dependency in order to use the hibernate validator with the maven project. It also requires the implementation of the Unified Expression Language for evaluating the dynamic expressions in the constraint violation messages.

Q39. What is the benefit of Hibernate Tools Eclipse plugin?

              

Q40. What is Hibernate? Explain with few advantages?

Q41. What is difference between Lazy Vs Eager fetching?

Q42. What is automatic dirty checking?

The dirty checking in the hibernate is used to automatically detect whether an object is modified or not and needs to be updated. With the dirty checking, all the necessary updating and changes are done without affecting other fields. It automatically detects the object states whenever changes and updates the database thereby saving the developers time.

Q43. For what Hibernate.cfg.xml file is used?

Q44. What is Session Factory?

Q45. What are various States of hibernate object?

Q46. What is the difference between the first and second level caches in Hibernate?

Q47. What is difference between update vs merge in Hibernate?

Q48. What is ORM? List the ORM levels available in Hibernate?

ORM (Object Relational Mapping) is used for converting data between the incompatible type systems. It uses an object-oriented language for converting the data.

The four levels of the ORM in Hibernate are,

Pure Relational ORM - In this level, the entire application is designed around the relational model.

Light Object Mapping - In this level, the entity classes are mapped manually to the relational tables. Applications that have less number of entities use this level.

Medium Object Mapping - In this level, the application is designed around an object model. Here, the associations between the objects are supported by the persistence mechanism.

Full Object Mapping - It is one of the most sophisticated objects modeling level. This level supports composition, inheritance, polymorphism, and persistence.

Q49. List few core interfaces of the Hibernate framework?

Q50. List the Collection types available in Hibernate ?

Q51. What are the Transaction Isolation Levels?

Transaction isolation determines the visibility of changes made by one transaction for another transaction without any interference with the user data. This process is done consistently without any skip in the process. For a high level of isolation, the database fixes lock on data.

Below are few isolation levels that are commonly used.

  • Read Uncommitted
  • Read Committed
  • Repeated read and
  • Serializable

Q52. How do you create a session object in Hibernate?

Q53. List some common JPA annotations used in Hibernate?

Q54. What is difference between Get vs Load in Hibernate?

Q55. Explain how Save is different from Persist?

Q56. What is HQL?

Q57. List the query types available in hibernate?

Q58. Define cascade and inverse option in one-many mapping?

Q59. What interface lets you write HQL?

Q60. Which annotation is used for sequences in Hibernate?

Q61. Bootstrapping in Hibernate is performed by which core interface?

Q62. How to create a transaction object in Hibernate?

Q63. What is a Named SQL Query?

Q64. Explain the differences between sorted and ordered collection in hibernate?

Q65. What are the ways to express joins in HQL?

Q66. What is Hibernate proxy?

Q67. List few advantages of Hibernate over jdbc?

Below are a few advantages of hibernate over JDBC.

  • Hibernate’s main responsibility is to solve the issues which are raised when a database is connected by any application. But in JDBC, fixing any kind of issue is tedious.
  • Code is written need to map the data in JDBC but in hibernate, the hibernate itself will map the data.
  • Hibernate’s HQL supports polymorphic queries but JDBC does not support.
  • Hibernate’s data is independent of data but JDBC depends on data to function.
  • Hibernate supports the concept of lazy loading but the concept of lazy loading is not incorporated in JDBC.

Q68. What is JPQL?

The Java Persistence Query Language (JPQL) is an object-oriented query language. It is defined as a part of the Java Persistence API (JPA) specification. This language is used to create queries against entities to store in a relational database. Based on the SQL syntax, JPQL uses SELECT, UPDATE, DELETE clause to perform database related operations.

Q69. What is difference between openSession and getCurrentSession?

Q70. For what saveOrUpdate() method is used in hibernate?

The saveOrUpdate() method is used to do either INSERT or UPDATE SQL query depending upon the object’s existence in the database. It first uses the SELECT query to determine if it needs to do INSERT or UPDATE operation. The saveOrUpdate method is also used to bring both the transient and the detached object into the persistent state.

Q71. List few Differences between session vs session factory in Hibernate?

Q72. How to run hibernate update query?

Q73. What is flush in hibernate?

The flush method in hibernate is used to force the session to flush. It synchronizes the session data with the database. By default, the hibernate will automatically flush changes for you before some query execution and when a transaction is committed.

About Author :

  • Author of Hibernate Interview Questions

    Calvin Carlo

    Calvin is a Senior web developer, a Javascript enthusiast, and a technical writer. He is very interested in learning new technologies and writing technical blogs on It. He loves playing Cricket, watching Hollywood action movies and is big fan of Michael Douglas.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.