Hibernate
Top 40 Hibernate Interview Questions and Answers 2024
44 questions found
Updated 10 Dec 2024
Q1. How do you define 'many-to-many' relationship in Hibernate when there are no common columns between two tables?
Many-to-many relationship in Hibernate without common columns
Create a third table with foreign keys to both tables
Use @ManyToMany annotation in both entity classes
Specify the join table name and column names in @JoinTable annotation
Q2. Difference between jdbc & hibernate
JDBC is a low-level API for connecting to databases, while Hibernate is a high-level ORM framework.
JDBC requires manual coding for CRUD operations, while Hibernate provides automatic mapping of objects to database tables.
JDBC is more suitable for small-scale applications, while Hibernate is better for large-scale applications.
Hibernate provides caching and lazy loading, which improves performance, while JDBC does not have these features.
Hibernate supports inheritance mapping,...read more
Q3. 6. Save vs Persist in hibernate ?
Save() method inserts a new record into the database, while persist() method can also be used to update an existing record.
save() method is used to insert a new record into the database
persist() method can also be used to insert a new record, but it can also be used to update an existing record
save() method returns the generated ID immediately, while persist() method does not
save() method can be called outside of a transaction, while persist() method must be called within a t...read more
Q4. How the Hibernate is configured in the Spring project?
Hibernate is configured in a Spring project using the HibernateTemplate or EntityManagerFactory.
Hibernate can be configured in a Spring project by defining the necessary beans in the application context XML file.
The HibernateTemplate class can be used to perform CRUD operations on the database using Hibernate.
Alternatively, the EntityManagerFactory can be configured to work with Hibernate in a Spring project.
The Hibernate properties such as database connection details, dialec...read more
Q5. Describe caching mechanism in hibernate
Hibernate provides two levels of caching: first-level cache and second-level cache.
First-level cache is enabled by default and is associated with a Session object.
Second-level cache is optional and can be configured to use different caching providers like Ehcache, Hazelcast, etc.
Caching can improve application performance by reducing the number of database queries.
Caching can also cause data inconsistency issues if not used properly.
Hibernate provides annotations like @Cachea...read more
Q6. What is Hibernate and why we are going for hibernate technology?
Hibernate is an ORM tool used to map Java objects to database tables.
Hibernate simplifies database access by abstracting the JDBC API
It provides a framework for mapping an object-oriented domain model to a relational database
Hibernate supports lazy loading, caching, and transaction management
It reduces the amount of boilerplate code needed for database operations
Hibernate is widely used in enterprise Java applications
Q7. How do handle lazy loading in hibernate
Lazy loading in Hibernate is a technique used to load data only when it is needed, improving performance.
Lazy loading is achieved by setting the fetch type to LAZY in the mapping of the entity relationship.
When an entity is loaded, associated entities marked as LAZY are not loaded until accessed.
Lazy loading helps in reducing unnecessary database calls and improves performance.
Example: @OneToMany(fetch = FetchType.LAZY)
Q8. * diff betwwen save n persist in hibernate
Difference between save and persist in Hibernate
Save method returns the generated ID immediately while persist method returns void
Save method can be called outside of a transaction while persist method must be called within a transaction
Save method can be used with detached objects while persist method cannot
Save method can be used with transient objects while persist method cannot
Hibernate Jobs
Q9. What is difference between get & load in hibernate
get() method returns null if object is not found in database, while load() method throws ObjectNotFoundException.
get() method is eager loading while load() method is lazy loading.
get() method returns a fully initialized object while load() method returns a proxy object.
get() method is slower than load() method as it hits the database immediately.
load() method is faster than get() method as it returns a proxy object and hits the database only when required.
Q10. How does Hibernate connects to DB
Hibernate connects to the database using JDBC (Java Database Connectivity) API.
Hibernate uses JDBC API to establish a connection to the database.
It uses JDBC drivers to communicate with the database.
Hibernate configuration file contains database connection details.
SessionFactory in Hibernate is responsible for creating sessions and managing connections.
Q11. What is hibernate, spring mvc.
Hibernate is an ORM tool while Spring MVC is a web framework for building Java applications.
Hibernate simplifies database access by mapping Java classes to database tables.
Spring MVC provides a model-view-controller architecture for building web applications.
Hibernate can be used with Spring MVC to handle database operations.
Spring MVC provides features like request mapping, view resolution, and form handling.
Both Hibernate and Spring MVC are widely used in Java development.
Q12. What is Hibernate and JPA?
Hibernate is an ORM tool and JPA is a specification for ORM.
Hibernate is an open-source ORM tool that maps Java classes to database tables.
JPA is a specification for ORM that defines a set of interfaces and rules for ORM tools to follow.
Hibernate implements the JPA specification and provides additional features.
Hibernate and JPA simplify database access by abstracting away SQL queries and providing a more object-oriented approach.
Example: Hibernate can be used to map a Java c...read more
Q13. How Transaction propagation works in Hibernate?
Transaction propagation in Hibernate allows the management of multiple database operations within a single transaction.
Hibernate supports different transaction propagation modes such as REQUIRED, REQUIRES_NEW, SUPPORTS, MANDATORY, NOT_SUPPORTED, and NEVER.
The propagation mode determines how the transaction should be handled when a method is called within an existing transaction.
REQUIRED is the default propagation mode, where if a transaction exists, the method will join it, o...read more
Q14. How to create database through the Hibernate
Database can be created through Hibernate by defining entity classes, mapping them to database tables, configuring Hibernate properties, and using Hibernate API to interact with the database.
Define entity classes representing tables in the database
Map entity classes to database tables using annotations or XML configuration
Configure Hibernate properties in a configuration file like hibernate.cfg.xml
Use Hibernate API to perform CRUD operations on the database
Q15. what is n+1 issue in hybernate ?
n+1 issue in Hibernate refers to the problem of excessive database queries being executed due to lazy loading.
Occurs when a query fetches an entity and its associated entities one at a time instead of all at once
Can be solved by using eager loading or batch fetching
Can also be solved by using a join fetch query
Can lead to performance issues and slow down the application
Q16. what is VPN what is the difference between hibernate and sleep mode
VPN stands for Virtual Private Network, a secure connection that allows users to access the internet privately and securely.
VPN creates a secure and encrypted connection over a less secure network, such as the internet.
It allows users to access resources on a remote network as if they were directly connected to that network.
VPN can be used to protect sensitive data, bypass geo-restrictions, and enhance privacy and security online.
Q17. Types of mapping in hibernate
Types of mapping in hibernate include one-to-one, one-to-many, many-to-one, and many-to-many mappings.
One-to-one mapping: Each instance of one entity is associated with exactly one instance of another entity.
One-to-many mapping: Each instance of one entity is associated with multiple instances of another entity.
Many-to-one mapping: Multiple instances of one entity are associated with exactly one instance of another entity.
Many-to-many mapping: Multiple instances of one entity...read more
Q18. rollback in hibernate
Rollback in Hibernate is used to undo the changes made to the database during a transaction.
Rollback is used to revert any changes made to the database within a transaction if an error occurs.
It is typically called in catch block of try-catch-finally block to handle exceptions.
Example: session.beginTransaction(); try { // database operations } catch (Exception e) { session.getTransaction().rollback(); }
Q19. Hibernate type of caches ?
Hibernate supports two types of caches: Level 1 (session) cache and Level 2 (SessionFactory) cache.
Level 1 cache is associated with the session object and is enabled by default.
Level 2 cache is shared across sessions and requires configuration.
Level 2 cache can be implemented using various providers like Ehcache, Infinispan, Hazelcast, etc.
Q20. Spring JPA vs Hibernate
Spring JPA is a part of the Spring Data project that makes it easier to work with JPA. Hibernate is a popular ORM framework.
Spring JPA is a higher level abstraction on top of JPA, providing more features and simplifying development.
Hibernate is a powerful ORM framework that provides mapping between Java objects and database tables.
Spring JPA can be used with Hibernate as the underlying ORM provider.
Hibernate offers more flexibility and control over the database interactions c...read more
Q21. What are different types of object relations we can have in Hibernate? Explain them
Different types of object relations in Hibernate include one-to-one, one-to-many, many-to-one, and many-to-many.
One-to-one: Each record in one table is related to only one record in another table.
One-to-many: Each record in one table can be related to multiple records in another table.
Many-to-one: Multiple records in one table can be related to only one record in another table.
Many-to-many: Multiple records in one table can be related to multiple records in another table.
Q22. 5. Lifecycle of object in hibernate ?
Hibernate manages object lifecycle through three states: transient, persistent, and detached.
When an object is created, it is in the transient state.
When the object is saved using session.save() method, it becomes persistent.
When the session is closed or the object is explicitly detached using session.evict() method, it becomes detached.
Detached objects can be re-attached to a new session using session.update() or session.merge() methods.
Objects can also be deleted using sess...read more
Q23. hybernet with class model
Hibernate is an ORM tool that maps Java classes to database tables.
Hibernate simplifies database access by abstracting the underlying SQL queries.
It provides a way to map Java classes to database tables using annotations or XML files.
Hibernate supports various types of associations between classes, such as one-to-one, one-to-many, and many-to-many.
It also supports caching to improve performance.
Hibernate can be integrated with Spring framework for easier configuration and man...read more
Q24. n+1 problem of hibernate
The n+1 problem in Hibernate occurs when a query results in multiple individual queries being executed for each row fetched.
Occurs when a query fetches a collection and then for each element in the collection, another query is executed to fetch related data
Can be resolved by using fetch joins or batch fetching to reduce the number of queries
Improves performance by reducing the number of database round trips
Q25. Difference between get session and load in hibernate
getSession() retrieves the current session while load() method loads the object by its identifier.
getSession() method is used to retrieve the current session from the session factory.
load() method is used to load the object by its identifier.
getSession() method returns null if there is no current session.
load() method throws an exception if the object is not found in the database.
getSession() method is used for read and write operations while load() method is used for read-on...read more
Q26. What is jdbc ? Different between jdbc and hibernate
JDBC is a Java API for connecting and executing queries on a database. Hibernate is an ORM framework that simplifies database interactions.
JDBC is a low-level API for database connectivity in Java, requiring manual handling of SQL queries and connections.
Hibernate is a high-level ORM framework that maps Java objects to database tables, abstracting away the need for manual SQL queries.
JDBC is more suitable for simple database operations, while Hibernate is preferred for comple...read more
Q27. What is hibernae in spring?
Hibernate in Spring is an ORM tool that simplifies database operations by mapping Java objects to database tables.
Hibernate is an ORM (Object-Relational Mapping) tool used in Spring framework
It simplifies database operations by mapping Java objects to database tables
It provides automatic generation of SQL queries based on Java classes and annotations
Q28. explain flow of hibernate using example
Hibernate is an ORM tool that simplifies database interactions in Java applications.
Hibernate configuration involves setting up database connection details in hibernate.cfg.xml
Entities are mapped to database tables using annotations or XML mapping files
SessionFactory is created to obtain Session objects for database operations
Transactions are managed using Session object to ensure data consistency
Example: Entity class 'User' mapped to 'users' table, fetching user data using H...read more
Q29. what is hibernate? ----answered
Hibernate is an open-source object-relational mapping framework for Java.
Hibernate simplifies the process of mapping Java objects to database tables.
It provides a framework for performing database operations using object-oriented programming concepts.
Hibernate supports automatic generation of SQL queries and provides caching mechanisms for improved performance.
It offers features like lazy loading, transaction management, and data retrieval using HQL (Hibernate Query Language)...read more
Q30. JPA vs Hibernate
JPA is a specification while Hibernate is an implementation of JPA.
JPA is a Java specification for managing relational data in applications.
Hibernate is an ORM framework that implements the JPA specification.
Hibernate provides additional features beyond JPA, such as caching and lazy loading.
JPA is vendor-neutral, allowing developers to switch between different JPA implementations.
Hibernate is a popular choice for JPA implementation due to its extensive features and community ...read more
Q31. JPA VS HIBERNATE WHAT IS ADVANTAGE USING JPA
JPA is a specification while Hibernate is an implementation of JPA. JPA provides a standard API for ORM frameworks.
JPA is a standard API for ORM frameworks
JPA provides a vendor-neutral interface
JPA allows for easy switching between ORM frameworks
Hibernate is an implementation of JPA
Hibernate provides additional features beyond JPA
Q32. hibernate many to many mapping design
Hibernate many to many mapping allows for a relationship where multiple instances of one entity are related to multiple instances of another entity.
Use @ManyToMany annotation in Hibernate to establish many to many relationship between entities
Create a join table to store the relationship between the two entities
Specify the mapping details such as cascade type, fetch type, and join column details
Q33. diff b/w get and load method in hibernate
get() method returns null if the object is not found in the database, while load() method throws an exception.
get() method is eager loading while load() method is lazy loading.
get() method hits the database immediately while load() method hits the database only when the object is accessed.
get() method returns a proxy object while load() method returns a real object.
get() method is used when we are not sure if the object exists in the database while load() method is used when ...read more
Q34. why hibernate is better then jdbc
Hibernate is better than JDBC due to its object-relational mapping capabilities, automatic table creation, and improved performance.
Hibernate provides object-relational mapping (ORM) which simplifies database interactions by mapping Java objects to database tables, eliminating the need for manual SQL queries.
Hibernate can automatically generate SQL queries based on the Java objects, reducing the amount of code that needs to be written.
Hibernate can create database tables auto...read more
Q35. What is Spring Hibernate?
Spring Hibernate is a framework that combines Spring and Hibernate to simplify the development of Java applications.
Spring Hibernate integrates the Spring framework with Hibernate ORM for database access.
It provides a way to manage transactions, data access, and other aspects of application development.
Developers can use annotations and XML configuration to define the mapping between Java objects and database tables.
It helps in reducing boilerplate code and improving producti...read more
Q36. Second level cache in hibernate?
Second level cache in Hibernate is used to cache data at the session factory level to reduce database hits.
Second level cache stores objects across sessions to reduce database calls
Improves performance by reducing database hits
Configurable cache providers like Ehcache, Infinispan can be used
Q37. Hibernate concepts with Jpa
Hibernate is a popular ORM framework that simplifies database interactions in Java applications by mapping Java objects to database tables.
Hibernate is an ORM (Object-Relational Mapping) framework that simplifies database operations in Java applications.
It provides a way to map Java objects to database tables and vice versa.
Hibernate supports JPA (Java Persistence API) annotations for defining mappings between Java classes and database tables.
It handles the database operation...read more
Q38. diff between get and load in hibernate
get() method retrieves data from the database immediately, while load() method returns a proxy object and loads data only when needed.
get() method returns null if the object is not found in the database, while load() method throws an ObjectNotFoundException.
get() method hits the database immediately, while load() method returns a proxy object without hitting the database until actual data is needed.
get() method is used when we are not sure if the object exists in the database...read more
Q39. JDBC vs HIBERNATE
JDBC is a low-level API for connecting to databases in Java, while Hibernate is a high-level ORM framework that simplifies database interactions.
JDBC requires writing SQL queries manually, while Hibernate provides an object-oriented approach to database operations.
Hibernate handles mapping Java objects to database tables, while JDBC requires manual mapping.
Hibernate provides caching mechanisms for improved performance, while JDBC does not have built-in caching.
JDBC is more su...read more
Q40. Hibernate with spring intergation and etc..
Hibernate is an ORM tool that integrates with Spring for database operations.
Hibernate provides a way to map Java objects to database tables.
Spring provides a framework for managing dependencies and integrating with other technologies.
Together, Hibernate and Spring can simplify database operations and improve performance.
Example: Using Hibernate with Spring, we can easily perform CRUD operations on a MySQL database.
Example: We can also use Spring's transaction management to e...read more
Q41. Hibernate concepts used in projects
Hibernate is used for ORM mapping in projects to interact with databases.
Mapping Java classes to database tables
Managing database connections
Performing CRUD operations
Implementing caching mechanisms
Supporting transactions
Q42. what is hibernet
Hibernate is an open-source object-relational mapping (ORM) framework for Java applications.
Hibernate simplifies the development of database-driven applications by mapping Java objects to database tables.
It provides a query language called HQL (Hibernate Query Language) for querying databases using object-oriented syntax.
Hibernate supports automatic generation of primary key values, caching, and transaction management.
Example: Hibernate can be used to map a Java class represe...read more
Q43. Whatis criteria in hibernate
Criteria in Hibernate is used to create and execute dynamic queries.
Criteria is an interface in Hibernate that allows the creation of dynamic queries without using HQL or SQL.
Criteria queries are type-safe and can be easily modified at runtime.
Criteria queries can be used to fetch entities based on certain conditions or restrictions.
Example: Criteria criteria = session.createCriteria(Employee.class);
Q44. Hibernate and its implementation
Hibernate is a popular Java ORM framework that simplifies database interactions by mapping Java objects to database tables.
Hibernate is an ORM (Object-Relational Mapping) framework that allows developers to map Java objects to database tables and vice versa.
It provides a way to perform database operations using Java objects without writing complex SQL queries.
Hibernate handles the mapping of Java classes to database tables, and Java data types to SQL data types.
It supports va...read more
Top Interview Questions for Related Skills
Interview Questions of Hibernate Related Designations
Interview experiences of popular companies
Reviews
Interviews
Salaries
Users/Month