questions_spring_hibernate
questions_spring_hibernate
1. What is inversion of control (IoC) and how does Spring implement it?
Inversion of Control is a design principle where the control of object creation and lifecycle is
transferred
to a container or framework. In Spring, IoC is achieved through dependency injection, where the
container manages the creation and wiring of objects (beans) and injects dependencies into them.
2. Explain the difference between constructor injection and setter injection in Spring.
injection involves using setter methods to set dependencies on a class after it has been instantiated.
objects are fully initialized when they're created, while setter injection is more suitable for optional
3. Explain the concept of aspect-oriented programming (AOP) and how it is implemented in Spring.
concerns, such as logging, transaction management, and security, that would otherwise result in
code
duplication. In Spring, AOP is implemented using aspects, which are modular units of cross-cutting
functionality. Spring AOP uses proxy-based or aspectJ-based mechanisms to apply aspects to target
objects at runtime.
4. What is Spring MVC, and how does it differ from Spring Boot?
Spring MVC is a web framework built on top of the Spring framework that provides support for
building
web applications using the Model-View-Controller pattern. It offers features such as controllers,
view
resolvers, and form handling. Spring Boot, on the other hand, is an opinionated framework for
building
is a part of the broader Spring ecosystem, Spring Boot simplifies the setup and deployment of Spring
applications.
5. Explain the role of the @Component, @Service, @Repository, and @Controller annotations in
Spring.
@Component: Marks a class as a Spring component, indicating that it will be automatically detected
and
registered as a bean.
Hibernate is an open-source Object-Relational Mapping (ORM) framework for Java that simplifies
the
development of database-driven applications by mapping Java objects to database tables and vice
versa.
Unlike JDBC, which requires developers to write SQL queries and handle database connections
manually,
Hibernate abstracts away the database interactions and provides a higher-level, object-oriented
interface
Simplified database access through the use of HQL (Hibernate Query Language) or Criteria API.
Automatic generation of SQL queries, reducing the need for manual query writing.
Database independence, allowing applications to work with different database systems without
modification.
8. Explain the difference between transient, persistent, and detached objects in Hibernate.
Transient objects are newly created objects that are not associated with any Hibernate session or
database record.
Persistent objects are objects that are associated with a Hibernate session and have a corresponding
database record. Changes made to persistent objects are automatically synchronized with the
database.
Detached objects are objects that were previously associated with a Hibernate session but are no
longer
actively managed. They may still contain data from the database, but changes made to detached
objects
are not automatically synchronized with the database unless they are reattached to a session.
Lazy loading is a technique used to delay the fetching of related entities until they are explicitly
accessed.
In Hibernate, lazy loading is achieved through proxy objects and bytecode instrumentation. When an
entity with lazy-loaded associations is loaded from the database, Hibernate substitutes the actual
collection or reference with a proxy object. The proxy object only loads the associated data from the
10. What is the difference between Hibernate's save() and persist() methods?
Both save() and persist() methods are used to save transient objects to the database. The main
difference between them is that persist() is part of the JPA specification, while save() is specific to
Hibernate. Additionally, persist() does not guarantee immediate execution of the SQL INSERT
statement,
while save() does. In practice, persist() is typically used for entities that are not yet associated with a
database identity, while save() is used when you need immediate feedback about the success of the
operation.
11. Explain the concept of Hibernate caching, and discuss its types.
First-level cache (session cache): Maintained at the session level, it caches objects retrieved within a
session.
Second-level cache: Shared across sessions and caches objects at the session factory level, providing
a