0% found this document useful (0 votes)
45 views8 pages

Spring & Hibernate (Question & Answers)

The document discusses Spring and Hibernate frameworks. Hibernate is an ORM tool that maps Java objects to database tables. Spring is a lightweight framework that provides infrastructure for developing Java applications and supports other frameworks like Hibernate. It describes the core components and architecture of Spring including the core container, data access/integration layer, web layer, and testing modules. It also summarizes the architecture of Hibernate including configuration, session factory, session, transaction, query, and criteria objects that are involved in a Hibernate application.

Uploaded by

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

Spring & Hibernate (Question & Answers)

The document discusses Spring and Hibernate frameworks. Hibernate is an ORM tool that maps Java objects to database tables. Spring is a lightweight framework that provides infrastructure for developing Java applications and supports other frameworks like Hibernate. It describes the core components and architecture of Spring including the core container, data access/integration layer, web layer, and testing modules. It also summarizes the architecture of Hibernate including configuration, session factory, session, transaction, query, and criteria objects that are involved in a Hibernate application.

Uploaded by

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

SPRING & HIBERNATE

(Question & Answers)

1. What is Hibernate?
Hibernate is an open source object relational mapping (ORM) tool that provides a
framework to map object-oriented domain models to relational databases for web
applications. Object relational mapping is based on the containerization of objects
and the abstraction that provides that capacity. Abstraction makes it possible to
address, access and manipulate objects without having to consider how they are
related to their data sources. The Hibernate ORM framework guides mapping Java
classes to database tables and Java data types to SQL data types and provides
querying and retrieval.

2. What is Spring?
Spring Framework is known as a lightweight framework due to its small size and
high effectiveness. It is open-source and thus provides a strong infrastructure to
develop Java applications in a simple and easy way. It provides support to different
other frameworks like Hibernate, Struts, EJB, etc. It is divided into certain modules
to achieve multiple things simultaneously. Spring modules include core module,
Web module, Data integration module, Test module, AOP module, etc. Each module
serves its own purpose as per the requirement of the developers.

3. Explain Spring Architecture.


The Spring Framework provides about 20 modules which can be used based on an
application requirement.
Spring Framework Architecture :
1. Core Container
The Core Container consists of the Core, Beans, Context, and Expression Language
modules the details of which are as follows −
• The Core module provides the fundamental parts of the framework,
including the IoC and Dependency Injection features.
• The Bean module provides BeanFactory, which is a sophisticated
implementation of the factory pattern.
• The Context module builds on the solid base provided by the Core and Beans
modules and it is a medium to access any objects defined and configured. The
ApplicationContext interface is the focal point of the Context module.
• The SpEL module provides a powerful expression language for querying and
manipulating an object graph at runtime.

2. Data Access/Integration
The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and
Transaction modules whose detail is as follows –
• The JDBC module provides a JDBC-abstraction layer that removes the need
for tedious JDBC related coding.
• The ORM module provides integration layers for popular object-relational
mapping APIs, including JPA, JDO, Hibernate, and iBatis.
• The OXM module provides an abstraction layer that supports Object/XML
mapping implementations for JAXB, Castor, XMLBeans, JiBX and XStream.
• The Java Messaging Service JMS module contains features for producing and
consuming messages.
• The Transaction module supports programmatic and declarative transaction
management for classes that implement special interfaces and for all your
POJOs.

3. Web
The Web layer consists of the Web, Web-MVC, Web-Socket, and Web-Portlet
modules the details of which are as follows –
• The Web module provides basic web-oriented integration features such as
multipart file-upload functionality and the initialization of the IoC container
using servlet listeners and a web-oriented application context.
• The Web-MVC module contains Spring's Model-View-Controller (MVC)
implementation for web applications.
• The Web-Socket module provides support for WebSocket-based, two-way
communication between the client and the server in web applications.
• The Web-Portlet module provides the MVC implementation to be used in a
portlet environment and mirrors the functionality of Web-Servlet module.

4. Miscellaneous
There are few other important modules like AOP, Aspects, Instrumentation, Web
and Test modules the details of which are as follows –
• The AOP module provides an aspect-oriented programming implementation
allowing you to define method-interceptors and pointcuts to cleanly
decouple code that implements functionality that should be separated.
• The Aspects module provides integration with AspectJ, which is again a
powerful and mature AOP framework.
• The Instrumentation module provides class instrumentation support and
class loader implementations to be used in certain application servers.
• The Messaging module provides support for STOMP as the WebSocket sub-
protocol to use in applications. It also supports an annotation programming
model for routing and processing STOMP messages from WebSocket clients.
5. Test
The Test module supports the testing of Spring components with JUnit or TestNG
frameworks.

6. Explain Hibernate Architecture.


Following section gives brief description of each of the class objects involved in
Hibernate Application Architecture.

Configuration Object
The Configuration object is the first Hibernate object you create in any Hibernate
application. It is usually created only once during application initialization. It
represents a configuration or properties file required by the Hibernate.
The Configuration object provides two keys components −
1. Database Connection − This is handled through one or more configuration
files supported by Hibernate. These files are hibernate.properties and
hibernate.cfg.xml.
2. Class Mapping Setup − This component creates the connection between the
Java classes and database tables.

SessionFactory Object
Configuration object is used to create a SessionFactory object which in turn
configures Hibernate for the application using the supplied configuration file and
allows for a Session object to be instantiated. The SessionFactory is a thread safe
object and used by all the threads of an application. The SessionFactory is a
heavyweight object; it is usually created during application start up and kept for
later use. You would need one SessionFactory object per database using a separate
configuration file. So, if you are using multiple databases, then you would have to
create multiple SessionFactory objects.

Session Object
A Session is used to get a physical connection with a database. The Session object is
lightweight and designed to be instantiated each time an interaction is needed with
the database. Persistent objects are saved and retrieved through a Session object.
The session objects should not be kept open for a long time because they are not
usually thread safe and they should be created and destroyed them as needed.

Transaction Object
A Transaction represents a unit of work with the database and most of the RDBMS
supports transaction functionality. Transactions in Hibernate are handled by an
underlying transaction manager and transaction (from JDBC or JTA). This is an
optional object and Hibernate applications may choose not to use this interface,
instead managing transactions in their own application code.

Query Object
Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data
from the database and create objects. A Query instance is used to bind query
parameters, limit the number of results returned by the query, and finally to execute
the query.

Criteria Object
Criteria objects are used to create and execute object oriented criteria queries to
retrieve objects.
7. What is ORM?
ORM stands for object-relational mapping, where objects are used to connect the
programming language on to the database systems, with the facility to work SQL
and object-oriented programming concepts. It is feasible for ORM to be
implemented on any type of database management system where object mapping to
the table can be achieved in the virtual system.

8. What are core interfaces of Hibernate?


1. Session Interface : The basic interface for all hibernate applications. The instances
are light weighted and can be created and destroyed without expensive process.

2. SessionFactory interface : The delivery of session objects to hibernate


applications is done by this interface. For the whole application, there will be
generally one SessionFactory and can be shared by all the application threads.

3. Configuration Interface : Hibernate bootstrap action is configured by this


interface. The location specification is specified by specific mapping documents, is
done by the instance of this interface.

4. Transaction Interface : This is an optional interface. This interface is used to


abstract the code from a transaction that is implemented such as a JDBC / JTA
transaction.

5. Query and Criteria interface : The queries from the user are allowed by this
interface apart from controlling the flow of the query execution.

9. What is Session and SessionFactory?


Session maintains a connection between the hibernate application and database. It
provides methods to store, update, delete or fetch data from the database such as
persist(), update(), delete(), load(), get() etc. It is a factory of Query, Criteria and
Transaction i.e. it provides factory methods to return these instances.

SessionFactory provides the instance of Session. It is a factory of Session. It holds


the data of second level cache that is not enabled by default.

10. Define Persistent classes.


Classes whose objects are stored in a database table are called as persistent classes.
11. Explain the use of Spring Container and different types of IOC container?
It is the core part and backbone of Spring framework. The Spring container helps to
create objects, combine the objects together, manage their configurations and
complete the life cycle of creation, implementation, and destruction. The Spring
container takes the help of Dependency Injection to manage the components which
build up the application.
IOC container is of two types as mentioned below:
1. Bean Factory– It is a simple container and provides support for dependency
injection.
2. Spring ApplicationContext– It is an advanced container that adds on complex
features like decode textual messages from files and is capable of publishing
events to the listeners.

12. What are the bean scope types?


Bean scope can be defined as singleton and prototype, request, session, global-
session, etc.

1. Prototype is declared when a new bean instance is required every time.


2. Singleton is declared when the same bean instance is used every time.
3. Request is used for the HTTP request scope.
4. Session is used for the HTTP session scope.
5. Global-HTTP session scopes the bean to the global HTTP session.
Syntax: <bean>

13. What is the Life Cycle of a Bean in Spring?


The life cycle of a Spring Bean follows certain steps, as mentioned below:
1. Instantiation – Spring container from the XML file finds the bean definition and
then the bean is instantiated.
2. Populate properties – Spring container populates all the properties mentioned in
the bean definition with the help of dependency injection.
3. Setting the name of Bean.
4. Setting the bean factory.
5. Pre-initialization and initialization of bean.
6. Post initialization of bean.
7. Destroy the bean by calling destroy () method.

14. What is hibernate configuration file?


Hibernate configuration file contains database specific configurations and used to
initialize SessionFactory. We provide database credentials or JNDI resource
information in the hibernate configuration xml file. Some other important parts of
hibernate configuration file is Dialect information, so that hibernate knows the
database type and mapping file or class details.

15. What is hibernate mapping file?


Hibernate mapping file is used to define the entity bean fields and database table
column mappings. We know that JPA annotations can be used for mapping but
sometimes XML mapping file comes handy when we are using third party classes
and we can’t use annotations.

16. What is difference between Hibernate Session get() and load() method?
Hibernate methods, get() and load() both are provided by the Session interface and
used to retrieve the objects from the database.
Session.get() :
The session.get() method is used to fetch a persistent object of the given class with
the given identifier. When we use the session.get() method, it will directly hit the
database and return the real object from the database. If no persistent object found
in the database, it returns null.
There are four get() methods available in Session interface with different
parameters:
1. get(Class class, Serializable id)
2. get(Class class, Serializable id, LockOptions lockOptions)
3. get(String entityName, Serializable id)
4. get(String entityName, Serializable id, LockOptions lockoptions)
All of the above methods perform the same functions.
Session.load() :
The session.load() method returns a 'proxy' object instead of a real object, without
hitting the database. A proxy is an object with the given identifier value, and its
properties are not initialized. It acts as a temporary object. If the persistent object is
not found, it will throw an exception called ObjectNotFoundException.
There are four load() methods available in the Session interface with different
parameters:
1. load(Class theClass, Serializable id)
2. load(Class theClass, Serializable id, LockOptions lockOptions)
3. load(String entityName, Serializable id)
4. load(String entityName, Serializable id, LockOptions lockOptions)
All of the above methods perform the same functions.
17. What are different states of an entity bean?
An entity bean instance can exist is one of the three states.
• Transient: When an object is never persisted or associated with any session,
it’s in transient state. Transient instances may be made persistent by calling
save(), persist() or saveOrUpdate(). Persistent instances may be made
transient by calling delete().
• Persistent: When an object is associated with a unique session, it’s in
persistent state. Any instance returned by a get() or load() method is
persistent.
• Detached: When an object is previously persistent but not associated with
any session, it’s in detached state. Detached instances may be made
persistent by calling update(), saveOrUpdate(), lock() or replicate(). The
state of a transient or detached instance may also be made persistent as a
new persistent instance by calling merge().

You might also like