Q1.What is Hibernate?
ANS: Hibernate is an open-source framework that makes it easier to store information in a relational database. It converts Java objects to corresponding database tables easily with ORM. It makes automatic database operations by CRUD (Create, Read, Update, and Delete) operations. Thus no need to write SQL queries as Hibernate provides a seamless mechanism and better-interacting features.
Q2. Advantages of using Hibernate?
ANS: Hibernate prevents data corruption as it ensures that the changes to the database are completely applied.
Hibernate provides its query language (HQL) which is similar to SQL.
HQL speeds up the performance and generates automatic database tables based on the Java class structure.
Q3. What is ORM in Hibernate?
ANS: ORM, which stands for Object-Relational Mapping acts as a translator that converts the Java object into the relational database. It makes it easier to store and retrieve information on Java objects, by using the ORM technique which reduces a lot of manual work.
Q4. List some of the databases supported by Hibernate.
ANS: MySQL
Oracle
PostgreSQL
H2
DB2
Microsoft SQL Server
Sybase
Q5. What are some of the important interfaces of the Hibernate framework?
ANS: The SessionFactory creates and manages the sessions that are occurring over a short conversation between your program and the database.
The session object provides an interface between the application and data stored in the database. It holds the first level cache(mandatory) of data. Sessions are the most important interfaces in the hibernate framework that gives you a tool like adding the data, changing the data, or getting data from the database and once you are done with the session, you need to close this session.
Transaction interface represents a database transaction including the committing changes to the database or the rollback.
The query interface is used for creating and executing the queries and retrieving the information from the database.
SessionFactoryBuilder is used to build a SessionFactory instance based on configuration settings for a new SessionFactory.
Q6. What is Java Persistence API (JPA)?
Java Persistence API is a collection of classes and methods to persist or store a vast amount of data in a database using ORM. JPA Persistence framework needs to follow:
Spring Data JPA: It reduces the amount of boilerplate code needed for common database operations like GET, PUT, POST, etc.
Spring Repository: It is an extension of Spring Repository which contains APIs for basic CRUD operations, pagination, and Sorting.
Q7. Explain the concept behind Hibernate Inheritance Mapping.
ANS: Inheritance Mapping in Hibernate is used for mapping object-oriented inheritance relationships into relational databases. It also ensures the structure of class hierarchies in programming while storing data in database tables. Hibernate uses the below tables for managing the mapping.
Single Table
Table Per Class
Joined Table Inheritance
Q8. What is a Session in Hibernate?
ANS: Session represents the single unit of work that acts as a gateway for interacting with the databases, Hibernate session is the primary interface for working with databases. The session provides various important functions such as
Transaction Management
Caching
Lazy Loading
Data Retrieval Strategies
Object Persistence and Retrieval
It also handles Data Relationship Handling where developers can manage and define relationships between objects, specifying fetching strategies, and overflow of behaviors.
Q9. What is SessionFactory?
ANS: SessionFactory in Hibernate is to create and manage the session instances that-
configure the management by reading
managing the configuration setting of the hibernate environment
database connection
mapping metadata (the data inside the data called metadata)
caching configurations.
When you create the SessionFactory it is shared among multiple threads within the application where each thread acquires its own Session instance shared from the SessionFactory.
Q10. What is the criteria API in Hibernate?
ANS: In hibernate it is a type-safe and programmatic way to make a database using Java code instead of native SQL queries or writing raw HQL (Hibernate Query Language) and helps make queries with dynamic sorting, projections, and difficult conditions. It also provides a smooth object-oriented approach to develop queries dynamically at runtime. Some advantages of using it include enhanced readability, minimizing the risk of syntax error in queries, and compile-time type checking. An explanation and example of how to use this in hibernate.
Q11. When is the merge() method of the hibernate session useful?
It is mainly used when you want to save the changes back from your previously detached(outside the scope of the Hibernate session) in the database. It is generally used to update a detached object back to the persistence context(managed state).
In Hibernate, an object could be in one of these three states:
Transient, persistent, detached
So, if you want to merge the detached object you can use the merge() method to reattach it in a new session and compile it with changes to reflect it into the database.
Q12. What are the most commonly used annotations available to support hibernate mapping?
ANS: @Entity:
It represents the database in the table and points to a Java class as an entity.
@Table:
It explains the details of the table related to the entity.
@Id:
@Id denotes the primary key of the entity.
@GeneratedValue:
It denotes how the primary key is made.
@Column:
In the database table, it plots a field to the column.
@OneToMany and @ManyToOne:
It settled a many-to-one or one-to-many relationship between entities.
@ManyToMany:
Defines a many-to-many relationship between entities. It explains the many-to-many relationship between the entities.
Browse our course links: Java Classes in Pune
To Join our FREE DEMO Session: Click Here
Q13. Does Hibernate support Native SQL Queries?
ANS: Yes, Hibernate supports Native SQL Queries which allows us to run SQL statements right against the database and we can use the createSQLQuery() method to use it in hibernate.
createSQLQuery(): It runs and recovers the results which return a query object that is customized with parameters.
Q14. What happens when the no-args constructor is absent in the Entity bean?
ANS: If the no-args constructor is absent, instantiation of objects by hibernate can’t happen properly which leads to errors. No-args constructor in hibernate is an entity bean that is very essential in the instantiation of objects in hibernate done by its reflection of constructing java objects and reading data from the database.
Q15. Can we declare the Entity class final?
ANS: Yes, you can declare an entity or class as ‘final’ in hibernate but it has some complications as it can’t be subclassed which affects hibernate’s ability to perform tasks like runtime enhancements and proxy generation for lazy loading for certain features.
Q16. What are the disadvantages of using Hibernate?
ANS: Hibernate also has some disadvantages, such as:
Learning curve: Hibernate is a complex framework, so it takes some time to learn how to use it effectively.
Performance: Hibernate can add some overhead to database access, so it is important to use it carefully to avoid performance problems.
Vendor lock-in: Hibernate is an open-source project, but it is developed and maintained by a single vendor. This can make it difficult to switch to a different ORM framework if necessary.
Q17. What is lazy loading in Hibernate?
ANS: Lazy loading is a technique that is used to defer the loading of an object’s child objects until they are actually needed. This can improve performance by reducing the number of database queries that need to be executed.
Lazy loading is enabled by default in Hibernate. However, it can be disabled for specific objects or relationships.
Q18. Is hibernate prone to SQL injection attacks?
ANS: SQL injection attack is a serious vulnerability in terms of web security wherein an attacker can interfere with the queries made by an application/website to its database thereby allowing the attacker to view sensitive data which are generally irretrievable. It can also give the attacker to modify/ remove the data resulting in damages to the application behavior.
Hibernate does not provide immunity to SQL Injection. However, following good practices avoids SQL injection attacks. It is always advisable to follow any of the below options:
Incorporate Prepared Statements that use Parameterized Queries.
Use Stored Procedures.
Ensure data sanity by doing input validation.
Q19. How do you create an immutable class in hibernate?
ANS: Immutable class in hibernate creation could be in the following way. If we are using the XML form of configuration, then a class can be made immutable by markingmutable=false. The default value is true there which indicates that the class was not created by default. In the case of using annotations, immutable classes in hibernate can also be created by using @Immutable annotation.
Q20. What do you think about the statement - “session being a thread-safe object”?
ANS: No, Session is not a thread-safe object which means that any number of threads can access data from it simultaneously.
Q21. What is a Session in Hibernate?
ANS: A session is an object that maintains the connection between Java object application and the database. Session also has methods for storing, retrieving, modifying or deleting data from the database using methods like persist(), load(), get(), update(), delete(), etc. Additionally, It has factory methods to return Query, Criteria, and Transaction objects.
Q22. Explain the hibernate mapping file
ANS: The Hibernate mapping file is an XML file that is used for defining the entity bean fields and corresponding database column mappings.
These files are useful when the project uses third-party classes where JPA annotations provided by hibernating cannot be used.
Q23. What does session?lock() method in hibernate do?
ANS: session.lock() method is used to reattach a detached object to the session. session.lock() method does not check for any data synchronization between the database and the object in the persistence context and hence this reattachment might lead to loss of data synchronization.
Q24. Collection mapping can be done using One-to-One and Many-to-One Associations. What do you think?
ANS: False, collection mapping is possible only with One-to-Many and Many-to-Many associations.
Q25. Does Hibernate support Native SQL Queries?
ANS: Yes, it does. Hibernate provides the createSQLQuery () method to let a developer call the native SQL statement directly and return a Query object.
Browse our course links: Java Classes in Pune
To Join our FREE DEMO Session: Click Here
Get More Information