Thursday, August 1, 2024

Top 50 Hibernate and JPA Interview Questions Answers for 3 to 5 Years Experienced Java developers

Hello guys, if you are preparing for Java developer interview then apart  from preparing core Java questions like Collections, Multithreading, Serialization, and Data Structures you should also prepare Spring Boot and Hibernate interview questions. Both Spring and Hibernate are two of the essential frameworks in Java world and its expected from an experienced Java developer to know them. I have given more than 50 interview in my career and in most of them I have seen questions from both Spring and Hibernate, especially on the jobs where Hibernate and Spring Framework is mentioned as required skills. 

While I have shared Spring Questions as well as Hibernate questions in the past, a lot of you asked me to share more Hibernate and JPA questions or practice so I have written this article where I have shared 50 questions. I have answered 22 of them and left 26 for you to answer.

This way, you can pay more attention, do more research and eventually prepare better for your Java and Hibernate developer interviews. 

50 Hibernate and JPA Interview Questions and Answers

Here are the common Hibernate and JPA interview questions for Java developers. Most of these question have also ben asked on various interviews and they cover important topics like how Hibernate works, Session management, caching, Querying, and relationship mapping in Hibernate. 


1. What is Hibernate? Why you use Hibernate?
Hibernate is an object relational framework also known as ORM, which helps to map a Java object to it's data representation. Hibernate is used in Java project at persistence or DAO layer. It takes care of storing object, loading object, caching them out-of-box. 

All you need to provide is a mapping file e.g. which object map to which table, which filed of object maps to which column of a table, and relationship between objects. Hibernate will take care of then populating objects. It also uses proxy objects to improve performance of Java application.

2. Is SessionFaactory thread-safe in Hibernate?
Yes, SessionFactory is thread-safe in Hibernate. It's is also Immutable. This is one of the tricky Hibernate interview question because developers often get's confused between Session vs SessionFactory and particularly when they hear thread-safety. 

3. What is a Session in Hibernate?
Session is another core object in Hibernate. It's the link between your application logic and Hibernate. Session is responsible for storing and loading object from database. All important methods e.g. get(), load(), save(), persist() and saveOrUpdate() belongs to Session object in Hibernate. 




4. Is a Session thread-safe in Hibernate?
No, Session is not thread-safe in Hibernate. It is meant to be used by just one thread and should not be shared.

5. What is difference between Session and SessionFactory in Hibernate?
This is one of the most frequently asked question from Hibernate interviews, mostly asked to 1 to 3 years experienced Hibernate developers. 

The main difference between Session and SessionFactory object in Hibernate is, as their name suggests, SessionFactory is used to create Session which is responsible for insert, update and delete operation. 

Another key difference between them is that first-level cache which is by default enabled is maintained at Session level while second level cache which is not enabled by default is maintained at SessionFacgtory level. 

6. What is first level cache in Hibernate?
First level cache is maintained at Session level. When you call the get() or load() method, an object is loaded by a Session. It cache that object for further reuse, So, next time when you request for same object from same session than it is served from first level cache. This is enabled by default in Hibernate to improve performance of Java application.

7. What is second level cache in Hibernate?
Second level cache is maintained at SessionFactory level and it's shared by all the Session. Hibernate search this cache for objects before going to database.


8. Does second level cache is enabled by default in Hibernate?
No, Second level cache is not enabled by default. You need to enable and configure it before use. This is the follow-up of previous Hibernate interview question. Another follow-up is how do you configure second level cache in Hibernate?

9. What is difference between first and second level cache in Hibernate?(answer)
Main difference between first and second level cache in Hibernate is that first level cache is maintained at Hibernate Session level, but second level cache is maintained at SessionFactory level, hence it's also shared by all Session. 

When a request to search an object is made e.g. via load() or get() method than Hibernate first search the first-level cache and when it doesn't found it goes to second level cache before hitting the database.

10. Which book you have read on Hibernate?
This is a really interesting question and I love to ask this one. Most common answer is that I have not read any book, I have just learned it by reading docs, blog posts etc. 

There is nothing wrong on that approach but I like candidate to read at least one book to get a complete and structured knowledge. Another common answer is that they have read Java Persistence with Hibernate, which I think is the best answer because the book is written by Gavin King which is also the author of Hibernate Framework. 

11. Suppose a record in the database, corresponding to a live object in Hibernate is updated using SQL. Does Hibernate will know about it? Does it will update the object?
No, any update which doesn't goes through Hibernate will have no effect on live object on Java application. Though you can use version to indicate that data is updated in database, so when Hibernate will try to save a stale version of object, it will fail. You can also update the cache manually to refresh the object in Java application.

12. How to enable logging of SQL in Hibernate?
By using show_sql property

13. What is difference between HQL (hibernate query language) and Criterion API?
Though both HQL and Criterion API allows you to query data in Hibernate and both are independent of database vendors, here are couple of differences between them:
1) HQL is more readable than Criterion and Restrictions
2) HQL uses String to specify query while Criterion uses Objects e.g. Restriction. This also means that it's possible to have compile time checking for Criterion queries which is not possible with HQL because they are only executed at runtime.
3) Criterion API is an object oriented way to query than String based HQL. 

14. What is Proxy in context of Hibernate?
Hibernate Framework uses Proxy to improve performance of Java application using hibernate. Proxy are replacement of original object which Hibernate uses to avoid hitting database until its absolutely required. 

When you call load() method with id then hibernate returns a Proxy, which just have id. When you access any method from this Proxy object e.g. getDescription() then it trigger database load for that object. 

You can use Proxy when you just need id i.e. primary or foreign key e.g. when you want to store a Book but you also need author_id, in this case you can call load(author_id) and you will get a proxy for Author object without hitting database. 

15. What is lazy loading of associations in context of Hibernate?
Another key feature of Hibernate is lazy loading of associations and relationship. By default Hibernate uses eager loading to initialize associations e.g. If you load an Employee object which also has reference to Department then department related data will also be loaded, even if its not required by application at that time. 

You can disable this feature and ask Hibernate to lazy load associations and relationship by using lazy=true in relationship statement e.g. many-to-one or one-to-many. This is called lazy loading of related object. It means when you load Employee then Department object will not be initialized. 

16. What are different kinds of relationship supported by Hibernate?
Hibernate supports one-to-one, one-to-many, many-to-one and many-to-many relationship.

17. When did an object moved to removed state?
When you called delete() or remove() method on the object from Persistent state.

18. What is difference between merge() and update() method in Hibernate?
The update() method is used to update the object when it is Persistent state but merge() can be used anytime to update the object. 

19. What is difference between session.update() and session.lock() in Hibernate?
Both update() and lock() method are used to reattach a detached object, but main difference between update() and lock() method is that lock() simply reattaches the object to the hibernate session without checking or updating the database. It assumes that database is in sync with detached object. 

It's best practice to use the Session.update() or Session.saveOrUpdate() method for reattaching a detached object. Use the lock() method only if you are absolutely sure that the detached object is in sync with database. 

20. When to use the list() and iterate() in Hibernate?
You should use iterate() method when objects are already in the session or second level cache but if objects are not in cache then use list() method. Iterate() performs better if objects exists in cache but will be slower than list() and might require multiple database hits for a simply query if object doesn't exists in cache.

21. How do you get the JDBC session from Hibernate Connection?
One of the latest question form hibernate phone interviews. Well, you can call the Session.connection() method to get the JDBC connection from Hibernate Session.

22. How would you reattach a detached objects to hibernate session when the same object has already been loaded? 
You can use the session.merge() method to reattach a detached object if already exists in Session or first level cache.


Hibernate and JPA Interview Questions for Practice

And, here are few more Hibernate interview questions for 3 to 5 years experienced Java developer to practice, if you know the answer, post in comments

23. What are some difference between JDBC and Hibernate?

24. What will happen if you call load(id) and the object doesn't exists in application?

25. What will happen when you call get(id) and object is not available in Java application?

26. What is difference between get() and load() method in Hibernate? (answer)

27. Which type of collection will you use, ordered or sorted if your dataset is large?

28. What is difference between ordered and sorted collection in Hibernate?

29. What is Hibernate Entity class?

30. Can a Hibernate Entity class be final in Java? (answer)

31. What is the requirement of Hibernate entity class? 

32. Why a default no-argument constructor is mandatory for Hibernate persistent class? What will happen if you don't provide a default no argument constructor?

33. What is the trick while overriding equals() method for an Hibernate entity class?

34. Why instanceof operator is preferred over getClass() in equals() method of Hibernate Persistence class?

35. What are different state on which a Hibernate object can reside? (answer)

36. What is difference between transient, persistent and detached state? (answer)

37. When does an object moves from transient to persistent state?

38. What is difference between save() and persist() method of Hibernate? (answer)

39. What is difference between the save() and saveOrUpdate() method?

40. What is the SessionFactory in Hibernate?

41. Can you tell me some uses of SessoinFactory? Roles and Responsibility?

42. What is query cache in Hibernate?

43. What is criterion API in Hibernate?

44. When do you use criterion query in Hibernate?

45. What are fetching strategies in Hibernate?

46. What is Hibernate Query language or HQL?

48. What is difference between SQL and HQL (Hibernate Query language)?

49. What is difference between Hibernate and JPA?

50. What are common JPA Annotations you have used in Past?

51. What is N + 1 query problem? How ill you solve it?

52. What is difference between Spring Data JPA and Hibernate?

53. What is difference between MyBatis and Hibernate?

If  you know the answers, post in comments and I will add them into main article. If you don't know the answer, let me know and I will also update it with the answers. 

Also here is a nice Hibernate Interview Cheat Sheet to remember the important Hibernate and JPA concepts for inteview:




That's all about the 50 Hibernate and JPA Interview Questions and Answers for Java interviews. As I said, both Spring Boot and Hibernate are essential Java framework and its expected from Java developers to know them. You can always expect few questions on Hibernate and Spring Boot on Java interviews, especially if the job description mention about these skills.  

These questions cover a lot of important Hibernate topics like lifecycle of entity object, how Hibernate works, caching in Hibernate, first level and second level cache, Session and Session Factory, thread-safety, Hibernate Query language, Criterion query, relationship modeling in Hibernate and others. 

I highly recommend Java developers to revise these topics before any Java interviews. 

Other Interview Questions resources for Java developers
  • 20 Spring Boot Interview questions for Java developers (Spring boot questions)
  • 25 Spring Security Interview Questions with answers (spring security questions)
  • 15 Spring Data JPA questions with answers (spring data JPA questions)
  • 133 Core Java Interview Questions with Answers (core java questions)
  • 5 Books to Learn Spring Framework for Beginners (books)
  • 15 Microservices Interview questions with answers (microservice questions)
  • 5 Spring and Hibernate Training Courses for Java developers (list)
  • 17 Spring AOP Interview Questions with Answers (spring AOP questions)
  • 2 Books to Learn Hibernate for Beginners (books)
  • 15 Spring Boot Interview Questions with Answers (spring boot questions)
  • 50 Thread Interview questions for Java programmers (multithreading questions)
  • 20 Spring MVC and REST Interview Questions with answers (spring mvc questions)

  • Thank you for reading so far. If you got any questions feel free to ask in comments. 

    No comments:

    Post a Comment