You are on page 1of 4

Hibernate Tricky Interview Questions & Answers

1. What does HQL stand for?

• Hibernate Query Language

2. What is SessionFactory?

• SessionFactory provides the instance of Session. It is a factory of Session. It holds the


data of second level cache that is not enabled by default.

3. How is HQL query created?

• The HQL query is created with the help of the following syntax: Session.createQuery

4. List the key components of Hibernate.

• Configuration
• Session
• SessionFactory
• Criteria
• Query
• Transaction

5. What is Session?

• It maintains a connection between the hibernate application and database.


• It provides methods to store, update, delete or fetch data from the database such as
persist(), update(), delete(), load(), get() etc.
• It is a factory of Query, Criteria and Transaction i.e. it provides factory methods to
return these instances.

6. How is SQL query created in Hibernate?

• The SQL query is created with the help of the following syntax:
Session.createSQLQuery

7. List some of the databases supported by Hibernate.

• Sybase SQL Server


• Informix Dynamic Server
• HSQL
• DB2
• MySQL
• Oracle
• PostgreSQL
• FrontBase

8. How can we add criteria to a SQL query?


• A criterion is added to a SQL query by using the Session.createCriteria.

9. Mention two components of Hibernate configuration object.

• Database Connection
• Class Mapping Setup

10. Is Session a thread-safe object?

• No, Session is not a thread-safe object, many threads can access it simultaneously. In
other words, you can share it between threads

11. 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.

12. What is the difference between session.save() and session.persist() method?

save() persist()
returns the identifier (Serializable) of the
Return nothing because its return type is void.
instance.
Syntax: public Serializable save(Object o) Syntax: public void persist(Object o)

13. How many types of association mapping are possible in hibernate?

• One to One
• One to Many
• Many to One
• Many to Many

14. What is hibernate?

• Hibernate is an open-source and lightweight ORM tool that is used to store,


manipulate, and retrieve data from the database.

15. What is lazy loading in hibernate?

• Lazy loading in hibernate improves the performance.


• It loads the child objects on demand.

16. What is a Session in Hibernate?

• A session is an object that maintains the connection between Java object application
and database. Session also has methods for storing, retrieving, modifying or deleting
data from database using methods like persist(), load(), get(), update(), delete(), etc.

17. What is the difference between first level cache and second level cache?
Second Level Cache First Level Cache
Second Level Cache is associated with
First Level Cache is associated with Session.
Session Factory.
It is not enabled by default. It is enabled by default.

18. Define criteria in terms of Hibernate.

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

19. What is automatic dirty checking in hibernate?

• The automatic dirty checking feature of Hibernate, calls update statement


automatically on the objects that are modified in a transaction.
• After changing the state, we are committing the transaction.

20. What is the difference between load and get method?

load() get()
Throws ObjectNotFoundException if an
Returns null if an object is not found.
object is not found.
load() method doesn't hit the database. get() method always hit the database.
It returns proxy object. It returns the real object, not the proxy.

21. 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".

22. Explain hibernate architecture?

• Hibernate architecture comprises of many interfaces such as Configuration,


SessionFactory, Session, Transaction, etc.

23. What are the states of the object in hibernate?

• Transient: The object is in a transient state if it is just created but has no primary key
(identifier) and not associated with a session.
• Persistent: The object is in a persistent state if a session is open, and you just saved
the instance in the database or retrieved the instance from the database.
• Detached: The object is in a detached state if a session is closed. After detached state,
the object comes to persistent state if you call lock() or update() method.

24. Mention some of the advantages of using ORM over JDBC.

• Generates key automatically.


• Details of SQL queries are hidden
• Application development is fast.
• Management of transaction.
25. What are the inheritance mapping strategies?

• Table per hierarchy


• Table per concrete class
• Table per subclass

26. What is the difference between merge and update method?

merge() method update() method


Merge means to combine something. Update means to edit something.
merge() should be used if you don't know the update() should be used if the session doesn't
state of the session, means you want to make contain an already persistent state with the
the modification at any time. same id.

27. What is advantage of HQL over SQL ?

• You don't need to learn SQL


• Database independent
• Simple to write a query

28. 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.

29. What is HQL (Hibernate Query Language)?

• Hibernate Query Language is known as an object-oriented query language.


• It is like a structured query language (SQL).

30. What are the core interfaces of Hibernate?

• Session
• Query
• Configuration
• Session Factory
• Criteria
• Transaction

You might also like