Below
are the Top 20 Hibernate Interview Questions based on the candidate's
experiences and the company's interview pattern.
A: An Object-Relational Mapping (ORM) tool helps to simplify data creation, manipulation, and access by internally using Java API to interact with the databases. It’s a technique that maps objects stored in a database.
2. What are the advantages of Hibernate?
1) It’s fast
2) It’s lightweight and open source
3) It reduces code length, removing boilerplate code, freeing up developers for other tasks
4) It strengthens the object-level relationship
5) It facilitates the generation of independent database queries
6) It provides resources for creating tables automatically
7) It’s easy to integrate with other Java Enterprise Edition (EE) frameworks.
Hibernate outclasses JDBC because:
1) Hibernate code is cleaner and more readable thanks to the elimination of boiler-plate code, something found in JDBC
2) Unlike JDBC API, Hibernate supports associations, collections, and inheritances
3) HQL (Hibernate Query Language) is closer to Java and is more object-oriented
4) Developers don’t need to write code to store and load data into the database
5) Hibernate enables faster application development
4. What are the core interfaces of Hibernate?
The core interfaces of Hibernate framework are:
1) Configuration
2) SessionFactory
3) Session
4) Query
5) Criteria
6) Transaction
The objects of criteria are used for the creation and execution of the object-oriented criteria queries.
6. 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.
7. Is SessionFactory a thread-safe object?
Yes, SessionFactory is a thread-safe object, many threads cannot access it simultaneously.
8. What is Session?
It maintains a connection between the hibernate application and the 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.
9. 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.
10. What is the
difference between session.save() and session.persist() method?
Sr. No |
Save() |
Persist() |
1 |
returns
the identifier (Serializable) of the instance. |
Return
nothing because its return type is void. |
2 |
public Serializable save(Object
o) |
public void persist(Object o) |
11. What is the
difference between get and load method?
Sr. No |
get() |
load() |
1 |
Returns null if an object is not
found. |
Throws ObjectNotFoundException if an object is not
found. |
2 |
get() method always hit the
database. |
load() method doesn't hit the
database. |
3 |
It returns the real object, not
the proxy. |
It returns proxy object. |
4 |
It should be used if you are
not sure about the existence of instance. |
It should be used if you are
sure that instance exists. |
There are 3 states of the object (instance) in hibernate.
1) Transient: The object is in a transient state if it is just created but has no primary key (identifier) and is not associated with a session.
2) 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.
3) Detached: The object is in a detached state if a session is closed. After the detached state, the object comes to a persistent state if you call lock() or update() method.
If you mark a class as mutable="false", the class will be treated as an immutable class. By default, it is mutable="true".
14. 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
15. 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.
16. What is the
difference between first-level cache and second-level cache?
Sr. No |
First Level Cache |
Second Level Cache |
1 |
First Level Cache is associated
with Session. |
Second Level Cache is
associated with SessionFactory. |
2 |
It is enabled by default. |
It is not enabled by default. |
Though both save() and saveOrUpdate() method is used to store an object into Database, the key difference between them is that save can only INSERT records but saveOrUpdate() can either INSERT or UPDATE records.
An interesting Hibernate interview question as you might have used both getCurrentSession() and openSession() to obtain an instance of the Session object. I have left this question unanswered for you to answer or find an answer based on your experience.
Hibernate query language, HQL is an object-oriented extension to SQL. It allows you to query, store, update, and retrieve objects from a database without using SQL.
A: Some design patterns include:
1) Data Mapper, which moves data between objects and a database, keeping them independent of each other and the mapper
2) Domain Model Pattern, which is a domain object model that incorporates both behavior and data
3) Proxy Pattern, for lazy loading
4) Factory pattern in SessionFactory
No comments:
Post a Comment