Spring & Hibernate (Question & Answers)
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.
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.
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.
5. Query and Criteria interface : The queries from the user are allowed by this
interface apart from controlling the flow of the query execution.
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().