Spring & Hibernate Interview Questions
Spring & Hibernate Interview Questions
Q. What are the different types of dependency injection? Explain with examples.
A: There are two types of dependency injection: setter injection and constructor
injection.
Setter Injection: Normally in all the java beans, we will use setter and getter
method to set and get the value of property as follows:
We will create an instance of the bean 'namebean' (say bean1) and set property
as bean1.setName("tom"); Here in setter injection, we will set the property
'name' in spring configuration file as shown below:
<bean id="bean1" class="namebean">
<property name="name" >
<value>tom</value>
</property>
</bean>
The subelement <value> sets the 'name' property by calling the set method as
setName("tom"); This process is called setter injection. To set properties that
reference other beans <ref>, subelement of <property> is used as shown below,
We will set the property 'name' while creating an instance of the bean 'namebean'
as namebean bean1 = new namebean("tom");
Q. What is spring? What are the various parts of spring framework? What are the
different persistence frameworks which could be used with spring?
A. Spring is an open source framework created to address the complexity of
enterprise application development. One of the chief advantages of the Spring
framework is its layered architecture, which allows you to be selective about
which of its components you use while also providing a cohesive framework for
J2EE application development. The Spring modules are built on top of the core
container, which defines how beans are created, configured, and managed, as
shown in the following figure. Each of the modules (or components) that comprise
the Spring framework can stand on its own or be implemented jointly with one or
more of the others. The functionality of each component is as follows:
The core container: The core container provides the essential functionality of the
Spring framework. A primary component of the core container is the BeanFactory,
an implementation of the Factory pattern. The BeanFactory applies the Inversion
of Control (IOC) pattern to separate an application’s configuration and
dependency specification from the actual application code.
Spring context: The Spring context is a configuration file that provides context
information to the Spring framework. The Spring context includes enterprise
services such as JNDI, EJB, e-mail, internalization, validation, and scheduling
functionality.
Spring AOP: The Spring AOP module integrates aspect-oriented programming
functionality directly into the Spring framework, through its configuration
management feature. As a result you can easily AOP-enable any object managed
by the Spring framework. The Spring AOP module provides transaction
management services for objects in any Spring-based application. With Spring
AOP you can incorporate declarative transaction management into your
applications without relying on EJB components.
Spring DAO: The Spring JDBC DAO abstraction layer offers a meaningful
exception hierarchy for managing the exception handling and error messages
thrown by different database vendors. The exception hierarchy simplifies error
handling and greatly reduces the amount of exception code you need to write,
such as opening and closing connections. Spring DAO’s JDBC-oriented exceptions
comply with its generic DAO exception hierarchy.
Spring ORM: The Spring framework plugs into several ORM frameworks to provide
its Object Relational tool, including JDO, Hibernate, and iBATIS SQL Maps. All of
these comply with Spring’s generic transaction and DAO exception hierarchies.
Spring Web module: The Web context module builds on top of the application
context module, providing contexts for Web-based applications. As a result, the
Spring framework supports integration with Jakarta Struts. The Web module also
eases the tasks of handling multi-part requests and binding request parameters
to domain objects.
Spring MVC framework: The Model-View-Controller (MVC) framework is a full-
featured MVC implementation for building Web applications. The MVC framework
is highly configurable via strategy interfaces and accommodates numerous view
technologies including JSP, Velocity, Tiles, iText, and POI.
Q. What is AOP? How does it relate with IOC? What are different tools to utilize
AOP?
A: Aspect-oriented programming, or AOP, is a programming technique that
allows programmers to modularize crosscutting concerns, or behavior that cuts
across the typical divisions of responsibility, such as logging and transaction
management. The core construct of AOP is the aspect, which encapsulates
behaviours’ affecting multiple classes into reusable modules. AOP and IOC are
complementary technologies in that both apply a modular approach to complex
problems in enterprise application development. In a typical object-oriented
development approach you might implement logging functionality by putting
logger statements in all your methods and Java classes. In an AOP approach you
would instead modularize the logging services and apply them declaratively to the
components that required logging. The advantage, of course, is that the Java
class doesn't need to know about the existence of the logging service or concern
itself with any related code. As a result, application code written using Spring AOP
is loosely coupled. The best tool to utilize AOP to its capability is AspectJ.
However AspectJ works at he byte code level and you need to use AspectJ
compiler to get the AOP features built into your compiled code. Nevertheless AOP
functionality is fully integrated into the Spring context for transaction
management, logging, and various other features. In general any AOP
framework control aspects in three possible ways:
<property name="mappingResources">
<list>
<value>org/appfuse/model/User.hbm.xml</value>
</list>
</property>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:db/appfuse</value>
</property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/appfuse</value>
</property>
</bean>
A Hibernate Session object represents a single unit-of-work for a given data store
and is opened by a SessionFactory instance. You must close Sessions when all
work for a transaction is completed. The following illustrates a typical Hibernate
session:
Session session = null;
UserInfo user = null;
Transaction tx = null;
try {
session = factory.openSession();
tx = session.beginTransaction();
user = (UserInfo)session.load(UserInfo.class, id);
tx.commit();
} catch(Exception e) {
if (tx != null) {
try {
tx.rollback();
} catch (HibernateException e1) {
throw new DAOException(e1.toString()); }
} throw new DAOException(e.toString());
} finally {
if (session != null) {
try {
session.close();
} catch (HibernateException e) { }
}
}
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
Q. What are the types of inheritence models and describe how they work like
vertical inheritence and horizontal?
A. There are three types of inheritance mapping in hibernate:
Example: Let us take the simple example of 3 java classes. Class Manager and
Worker are inherited from Employee Abstract class.
1. Table per concrete class with unions : In this case there will be 2 tables.
Tables: Manager, Worker [all common attributes will be duplicated]
2. Table per class hierarchy: Single Table can be mapped to a class hierarchy.
There will be only one table in database called 'Employee' that will represent all
the attributes required for all 3 classes. But it needs some discriminating column
to differentiate between Manager and worker;
3. Table per subclass: In this case there will be 3 tables represent Employee,
Manager and Worker