Some of the Hibernate Interview Questions for beginners and professionals.
Q1. What is Hibernate?
Hibernate is one of the most popular Java frameworks that simplify the development of Java applications to interact with the database. Hibernate also provides a reference implementation of Java API. In other words, Hibernate is an open-source and lightweight ORM tool that is used to store, manipulate, and retrieve data from the database.
Q2. What is ORM?
ORM is an acronym for Object/Relational mapping. It is a programming strategy to map objects with the data stored in the database. It simplifies data creation, data manipulation, and data access.
Q3. What are the major advantages of Hibernate Framework?
1). It is open-sourced and lightweight.
2). The performance of Hibernate is very fast.
3). Provides facilities to automatically create a table.
4). It provides query statistics and database status.
Q4. Mentions core interfaces of Hibernate.
The core interfaces of Hibernate framework are:
Configuration
SessionFactory
Session
Query
Criteria
Transaction
Q5. What are the advantages of using ORM over JDBC?
Application development is fast. Hibernate eliminates a lot of boiler-plate code that comes with JDBC API, the code looks cleaner and readable. Management of transactions. Generates key automatically. Details of SQL queries are hidden.
Q6. What is the difference between first-level cache and second-level cache?
First Level Cache is associated with Session while Second Level Cache is associated with SessionFactory. First Level cache is enabled by default. The second-level cache is not enabled by default.
Q7. Why use Hibernate Framework?
It overcomes the database dependency faced in the JDBC. Changing the databases costs a lot working on JDBC, hibernate overcomes this problem with flying colors. Code portability is not an option while working on JDBC. This is easily handled by Hibernate. Hibernate strengthens the object-level relationship. It overcomes the exception-handling part which is mandatory while working on JDBC.
Q8. What are the different functionalities supported by Hibernate?
1). Hibernate is an ORM tool.
2). Hibernate uses Hibernate Query Language(HQL) which makes it database-independent.
3). It supports auto DDL operations.
4). Supports cache memory.
5). Exception handling is not mandatory in the case of Hibernate.
Q9. What is HQL?
HQL is the acronym for Hibernate Query Language. It is an Object-Oriented Query Language and is independent of the database.
Q10. Explain Hibernate Proxy and how it helps in Lazy loading.
Hibernate uses a proxy object in order to support Lazy loading. When you try loading data from tables, Hibernate doesn’t load all the mapped objects. After you reference a child object through getter methods, if the linked entity is not present in the session cache, then the proxy code will be entered into the database and load the linked object.
Q11. What is Named SQL Query?
Hibernate provides another important feature called Named Query using which you can define at a central location and use anywhere in the code. You can create named queries for both HQL as well as for Native SQL. These Named Queries can be defined in Hibernate mapping files with the help of JPA annotations @NamedQuery and @NamedNativeQuery.
Q12. When do you use merge() and update() in Hibernate?
update(): If you are sure that the Hibernate Session does not contain an already persistent instance with the same id.
merge(): Helps in merging your modifications at any time without considering the state of the Session.
Q13. Difference between get() vs load() method in Hibernate?
load(): It will throw an exception if an object with an ID passed to them is not found.
get(): Will return null.
load(): It can return proxy without hitting the database unless required.
get(): It always goes to the database.
Q14. Difference between Session and SessionFactory in Hibernate?
A Session is a single-threaded, short-lived object. It provides the first-level cache.
SessionFactory is immutable and shared by all Session. It also lives until the Hibernate is running. It also provides the second-level cache.
Q15. Difference between save() and saveOrUpdate() method of Hibernate?
Even though the save() and saveOrUpdate() method is used to store an object in the Database, the key difference between them is that save() can only Insert records but saveOrUpdate() can either Insert or Update records.
Q16. Difference between sorted and ordered collection in Hibernate?
sorted collection sort the data in JVM’s heap memory using Java’s collection framework sorting methods. The ordered collection is sorted using an order by clause in the database itself.
Q17. What difference is between the transient, persistent, and detached state in Hibernate?
Transient state: New objects are created in the Java program but are not associated with any Hibernate Session.
Persistent state: An object which is associated with a Hibernate session is called a Persistent object.
Detached state: An object which was earlier associated with Hibernate session but currently it’s not associated with is known as a detached object.
Q18. Difference between managed associations and Hibernate associations?
Managed associations: Relate to container management persistence and are bi-directional.
Hibernate Associations: These associations are unidirectional.
Q19. List some of the databases supported by Hibernate.
Some of the databases supported by Hibernate are
DB2
MySQL
Oracle
Sybase SQL Server
Informix Dynamic Server
HSQL
PostgreSQL
FrontBase
Q20. What does HQL stand for?
Hibernate Query Language
Q21. Define persistent classes.
Classes whose objects are stored in a database table are called persistent classes.
Q22. What is SessionFactory?
SessionFactory provides the instance of Session. It is a factory of Session. It holds the data of the second-level cache that is not enabled by default.
Q23. Is SessionFactory a thread-safe object?
Yes, SessionFactory is a thread-safe object, many threads cannot access it simultaneously.
Q24. What is Session?
1). It maintains a connection between the hibernate application and the database.
2). It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.
3). It is a factory of Query, Criteria, and Transaction i.e. it provides factory methods to return these instances.
Q25. Is Session a thread-safe object?
No, Session is not a thread-safe object, many threads can access it simultaneously.
No comments:
Post a Comment