0% found this document useful (0 votes)
22 views4 pages

questions_spring_hibernate

Java Program interview question

Uploaded by

sshinde1622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

questions_spring_hibernate

Java Program interview question

Uploaded by

sshinde1622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

interview questions on 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.

Constructor injection involves passing dependencies as arguments to a class's constructor, while


setter

injection involves using setter methods to set dependencies on a class after it has been instantiated.

Constructor injection is generally considered preferable for mandatory dependencies, as it ensures


that

objects are fully initialized when they're created, while setter injection is more suitable for optional

dependencies or for circular dependencies.

3. Explain the concept of aspect-oriented programming (AOP) and how it is implemented in Spring.

Aspect-oriented programming is a programming paradigm that allows you to modularize cross-


cutting

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

stand-alone, production-grade Spring-based applications with minimal configuration. While Spring


MVC

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.

These annotations are used to declare Spring-managed beans within an application:

@Component: Marks a class as a Spring component, indicating that it will be automatically detected
and

registered as a bean.

@Service: Specialization of @Component used to annotate classes representing business services.

@Repository: Specialization of @Component used to annotate classes representing data access

components (typically DAOs).

@Controller: Specialization of @Component used to annotate classes representing web controllers


in a

Spring MVC application.

6. What is Hibernate, and how does it differ from JDBC?

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

for data persistence.

7. What are the key benefits of using Hibernate?

Some key benefits of using Hibernate include:

Automatic mapping of Java objects to database tables (and vice versa).

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.

Built-in caching mechanisms for improved performance.

Support for transaction management and database operations.

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.

9. What is lazy loading in Hibernate, and how does it work?

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

database when it is accessed for the first time.

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.

Hibernate caching is a mechanism used to improve the performance of database operations by


storing

frequently accessed data in memory. Hibernate supports several types of caching:

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

broader scope of caching.

You might also like