0% found this document useful (0 votes)
221 views64 pages

More About Model-View-Controller Architecture : Struts1.1

The document provides an overview of the Struts 1.1 framework. It discusses key concepts like MVC architecture, frameworks, and the core components of Struts including the ActionServlet controller, ActionForms for handling form data, Action classes for processing business logic, and ActionMappings for mapping requests to actions. It also covers design patterns used in Struts and the typical directory structure of a Struts application.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
221 views64 pages

More About Model-View-Controller Architecture : Struts1.1

The document provides an overview of the Struts 1.1 framework. It discusses key concepts like MVC architecture, frameworks, and the core components of Struts including the ActionServlet controller, ActionForms for handling form data, Action classes for processing business logic, and ActionMappings for mapping requests to actions. It also covers design patterns used in Struts and the typical directory structure of a Struts application.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 64

Struts1.

1
1.What is MVC?
Model-View-Controller (MVC) is a design pattern put together to help control change. MVC
decouples interface from business logic and data.

• Model : The model contains the core of the application's functionality. The model
encapsulates the state of the application. Sometimes the only functionality it contains
is state. It knows nothing about the view or controller.
• View: The view provides the presentation of the model. It is the look of the
application. The view can access the model getters, but it has no knowledge of the
setters. In addition, it knows nothing about the controller. The view should be notified
when changes to the model occur.
• Controller:The controller reacts to the user input. It creates and sets the model.

More about Model-View-Controller Architecture >>

2.What is a framework?
A framework is made up of the set of classes which allow us to use a library in a best possible
way for a specific requirement.
3.What is Struts framework?
Struts framework is an open-source framework for developing the web applications in Java EE,
based on MVC-2 architecture. It uses and extends the Java Servlet API. Struts is robust
architecture and can be used for the development of application of any size. Struts framework
makes it much easier to design scalable, reliable Web applications with Java.
4.What are the components of Struts?
Struts components can be categorize into Model, View and Controller:

• Model: Components like business logic /business processes and data are the part of
model.
• View: HTML, JSP are the view components.
• Controller: Action Servlet of Struts is part of Controller components which works as
front controller to handle all the requests.

5.What are the core classes of the Struts Framework?


Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2
design.

• JavaBeans components for managing application state and behavior.


• Event-driven development (via listeners as in traditional GUI development).
• Pages that represent MVC-style views; pages reference view roots via the JSF
component tree.

6.What is ActionServlet?
ActionServlet is a simple servlet which is the backbone of all Struts applications. It is the main
Controller component that handles client requests and determines which Action will process
each received request. It serves as an Action factory – creating specific Action classes based on
user’s request.

1
7.What is role of ActionServlet?
ActionServlet performs the role of Controller:

• Process user requests


• Determine what the user is trying to achieve according to the request
• Pull data from the model (if necessary) to be given to the appropriate view,
• Select the proper view to respond to the user
• Delegates most of this grunt work to Action classes
• Is responsible for initialization and clean-up of resources

8.What is the ActionForm?


ActionForm is javabean which represents the form inputs containing the request parameters
from the View referencing the Action bean.

9.What are the important methods of ActionForm?


The important methods of ActionForm are : validate() & reset().

10.Describe validate() and reset() methods ?


validate() : Used to validate properties after they have been populated; Called before
FormBean is handed to Action. Returns a collection of ActionError as ActionErrors.
Following is the method signature for the validate() method.

public ActionErrors validate(ActionMapping


mapping,HttpServletRequest request)

reset(): reset() method is called by Struts Framework with each request that uses the defined
ActionForm. The purpose of this method is to reset all of the ActionForm's data members prior
to the new request values being set.

public void reset() {}

11.What is ActionMapping?
Action mapping contains all the deployment information for a particular Action bean. This class
is to determine where the results of the Action will be sent once its processing is complete.

12.How is the Action Mapping specified ?


We can specify the action mapping in the configuration file called struts-config.xml. Struts
framework creates ActionMapping object from <ActionMapping> configuration element of
struts-config.xml file

<action-mappings>
<action path="/submit"
type="submit.SubmitAction"
name="submitForm"

2
input="/submit.jsp"
scope="request"
validate="true">
<forward name="success" path="/success.jsp"/>
<forward name="failure" path="/error.jsp"/>
</action>
</action-mappings>

13.What is role of Action Class?


An Action Class performs a role of an adapter between the contents of an incoming HTTP
request and the corresponding business logic that should be executed to process this request.

14.In which method of Action class the business logic is executed ?


In the execute() method of Action class the business logic is executed.

public ActionForward execute(


ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception ;

execute() method of Action class:

• Perform the processing required to deal with this request


• Update the server-side objects (Scope variables) that will be used to create the next
page of the user interface
• Return an appropriate ActionForward object

15.What design patterns are used in Struts?


Struts is based on model 2 MVC (Model-View-Controller) architecture. Struts controller uses the
command design pattern and the action classes use the adapter design pattern. The

process() method of the RequestProcessor uses the template method design pattern. Struts
also implement the following J2EE design patterns.

• Service to Worker
• Dispatcher View
• Composite View (Struts Tiles)
• Front Controller
• View Helper
• Synchronizer Token

3
16.Can we have more than one struts-config.xml file for a single Struts application?
Yes, we can have more than one struts-config.xml for a single Struts application. They can be
configured as follows:

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml,
/WEB-INF/struts-admin.xml,
/WEB-INF/struts-config-forms.xml
</param-value>
</init-param>
.....
<servlet>

17.What is the directory structure of Struts application?


The directory structure of Struts application :

4
18.What is the difference between session scope and request scope when saving
formbean ?
when the scope is request,the values of formbean would be available for the current request.
when the scope is session,the values of formbean would be available throughout the session.
19.What are the important tags of struts-config.xml ?
The five important sections are:

5
20.What are the different kinds of actions in Struts?
The different kinds of actions in Struts are:

• ForwardAction
• IncludeAction
• DispatchAction
• LookupDispatchAction
• SwitchAction

21.What is DispatchAction?
The DispatchAction class is used to group related actions into one class. Using this class, you
can have a method for each logical action compared than a single execute method. The
DispatchAction dispatches to one of the logical actions represented by the methods. It picks a

6
method to invoke based on an incoming request parameter. The value of the incoming
parameter is the name of the method that the DispatchAction will invoke.

22.How to use DispatchAction?


To use the DispatchAction, follow these steps :

• Create a class that extends DispatchAction (instead of Action)


• In a new class, add a method for every function you need to perform on the service –
The method has the same signature as the execute() method of an Action class.
• Do not override execute() method – Because DispatchAction class itself provides
execute() method.
• Add an entry to struts-config.xml

DispatchAction Example »

23.What is the use of ForwardAction?


The ForwardAction class is useful when you’re trying to integrate Struts into an existing
application that uses Servlets to perform business logic functions. You can use this class to take
advantage of the Struts controller and its functionality, without having to rewrite the existing
Servlets. Use ForwardAction to forward a request to another resource in your application,
such as a Servlet that already does business logic processing or even another JSP page. By using
this predefined action, you don’t have to write your own Action class. You just have to set up
the struts-config file properly to use ForwardAction.

24.What is IncludeAction?
The IncludeAction class is useful when you want to integrate Struts into an application that
uses Servlets. Use the IncludeAction class to include another resource in the response to the
request being processed.

25.What is the difference between ForwardAction and IncludeAction?


The difference is that you need to use the IncludeAction only if the action is going to be
included by another action or jsp. Use ForwardAction to forward a request to another
resource in your application, such as a Servlet that already does business logic processing or
even another JSP page.

26.What is LookupDispatchAction?
The LookupDispatchAction is a subclass of DispatchAction. It does a reverse lookup on the
resource bundle to get the key and then gets the method whose name is associated with the
key into the Resource Bundle.

27.What is the use of LookupDispatchAction?


LookupDispatchAction is useful if the method name in the Action is not driven by its name in
the front end, but by the Locale independent key into the resource bundle. Since the key is
always the same, the LookupDispatchAction shields your application from the side effects of
I18N.

28.What is difference between LookupDispatchAction and DispatchAction? •

7
The difference between LookupDispatchAction and DispatchAction is that the actual method
that gets called in LookupDispatchAction is based on a lookup of a key value instead of
specifying the method name directly.

29.What is SwitchAction?
The SwitchAction class provides a means to switch from a resource in one module to another
resource in a different module. SwitchAction is useful only if you have multiple modules in your
Struts application. The SwitchAction class can be used as is, without extending.

30.What if <action> element has <forward> declaration with same name as global forward?
In this case the global forward is not used. Instead the <action> element’s <forward> takes
precendence.

31.What is DynaActionForm?
A specialized subclass of ActionForm that allows the creation of form beans with dynamic sets
of properties (configured in configuration file), without requiring the developer to create a
Java class for each type of form bean.

32.What are the steps need to use DynaActionForm?


Using a DynaActionForm instead of a custom subclass of ActionForm is relatively
straightforward. You need to make changes in two places:

• In struts-config.xml: change your <form-bean> to be an


org.apache.struts.action.DynaActionForm instead of some subclass of ActionForm

<form-bean
name="loginForm"type="org.apache.struts.action.DynaActionForm" >
<form-property name="userName" type="java.lang.String"/>
<form-property name="password" type="java.lang.String" />
</form-bean>

• In your Action subclass that uses your form bean:


o import org.apache.struts.action.DynaActionForm
o downcast the ActionForm parameter in execute() to a DynaActionForm
o access the form fields with get(field) rather than getField()

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;

8
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import org.apache.struts.action.DynaActionForm;

public class DynaActionFormExample extends Action {


public ActionForward execute(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm loginForm = (DynaActionForm) form;
ActionMessages errors = new ActionMessages();

if (((String) loginForm.get("userName")).equals("")) {
errors.add("userName", new ActionMessage(
"error.userName.required"));
}
if (((String) loginForm.get("password")).equals("")) {
errors.add("password", new ActionMessage(
"error.password.required"));
}
...........

33.How to display validation errors on jsp page?


<html:errors/> tag displays all the errors. <html:errors/> iterates over ActionErrors request
attribute.

34.What are the various Struts tag libraries?


The various Struts tag libraries are:

• HTML Tags
• Bean Tags
• Logic Tags
• Template Tags
• Nested Tags
• Tiles Tags

9
35.What is the use of <logic:iterate>?
<logic:iterate> repeats the nested body content of this tag over a specified collection.

<table border=1>
<logic:iterate id="customer" name="customers">
<tr>
<td><bean:write name="customer" property="firstName"/></td>

<td><bean:write name="customer" property="lastName"/></td>


<td><bean:write name="customer" property="address"/></td>
</tr>
</logic:iterate>
</table>

36.What are differences between <bean:message> and <bean:write>


<bean:message>: is used to retrive keyed values from resource bundle. It also supports the
ability to include parameters that can be substituted for defined placeholders in the retrieved
string.
<bean:message key="prompt.customer.firstname"/>

<bean:write>: is used to retrieve and print the value of the bean property. <bean:write> has
no body.
<bean:write name="customer" property="firstName"/>

37.How the exceptions are handled in struts?


Exceptions in Struts are handled in two ways:

• Programmatic exception handling : Explicit try/catch blocks in any code that can
throw exception. It works well when custom value (i.e., of variable) needed when error
occurs.

• Declarative exception handling :You can either define <global-exceptions> handling


tags in your struts-config.xml or define the exception handling tags within
<action></action> tag. It works well when custom page needed when error occurs.
This approach applies only to exceptions thrown by Actions.

<global-exceptions>
<exception key="some.key"
type="java.lang.NullPointerException"
path="/WEB-INF/errors/null.jsp"/>
</global-exceptions>

10
or
<exception key="some.key"
type="package.SomeException"
path="/WEB-INF/somepage.jsp"/>

38.What is difference between ActionForm and DynaActionForm?

• An ActionForm represents an HTML form that the user interacts with over one or more
pages. You will provide properties to hold the state of the form with getters and
setters to access them. Whereas, using DynaActionForm there is no need of providing
properties to hold the state. Instead these properties and their type are declared in
the struts-config.xml

• The DynaActionForm bloats up the Struts config file with the xml based definition. This
gets annoying as the Struts Config file grow larger.

• The DynaActionForm is not strongly typed as the ActionForm. This means there is no
compile time checking for the form fields. Detecting them at runtime is painful and
makes you go through redeployment.

• ActionForm can be cleanly organized in packages as against the flat organization in the
Struts Config file.

• ActionForm were designed to act as a Firewall between HTTP and the Action classes,
i.e. isolate and encapsulate the HTTP request parameters from direct use in Actions.
• With DynaActionForm, the property access is no different than using
request.getParameter( .. ).

• DynaActionForm construction at runtime requires a lot of Java Reflection
(Introspection) machinery that can be avoided.

39.How can we make message resources definitions file available to the o


Struts framework environment?
We can make message resources definitions file (properties file) available to Struts framework
environment by adding this file to struts-config.xml.
<message-resources
parameter="com.login.struts.ApplicationResources"/>

40.What is the life cycle of ActionForm?


The lifecycle of ActionForm invoked by the RequestProcessor is as follows:

• Retrieve or Create Form Bean associated with Action


• "Store" FormBean in appropriate scope (request or session)
• Reset the properties of the FormBean
• Populate the properties of the FormBean
• Validate the properties of the FormBean
• Pass FormBean to Action

11
Spring

1. What is IOC (or Dependency Injection)?

The basic concept of the Inversion of Control pattern (also known as dependency injection) is
that you do not create your objects but describe how they should be created. You don't
directly connect your components and services together in code but describe which services
are needed by which components in a configuration file. A container (in the case of the Spring
framework, the IOC container) is then responsible for hooking it all up.

i.e., Applying IoC, objects are given their dependencies at creation time by some external
entity that coordinates each object in the system. That is, dependencies are injected into
objects. So, IoC means an inversion of responsibility with regard to how an object obtains
references to collaborating objects.
2. What are the different types of IOC (dependency injection) ?
There are three types of dependency injection:

• Constructor Injection (e.g. Pico container, Spring etc): Dependencies are provided as
constructor parameters.
• Setter Injection (e.g. Spring): Dependencies are assigned through JavaBeans properties
(ex: setter methods).
• Interface Injection (e.g. Avalon): Injection is done through an interface. Note: Spring
supports only Constructor and Setter Injection

3. What are the benefits of IOC (Dependency Injection)?


Benefits of IOC (Dependency Injection) are as follows:
• Minimizes the amount of code in your application. With IOC containers you do not care
about how services are created and how you get references to the ones you need. You
can also easily add additional services by adding a new constructor or a setter method
with little or no extra configuration.
• Make your application more testable by not requiring any singletons or JNDI lookup
mechanisms in your unit test cases. IOC containers make unit testing and switching
implementations very easy by manually allowing you to inject your own objects into
the object under test.
• Loose coupling is promoted with minimal effort and least intrusive mechanism. The
factory design pattern is more intrusive because components or services need to be
requested explicitly whereas in IOC the dependency is injected into requesting piece of
code. Also some containers promote the design to interfaces not to implementations
design concept by encouraging managed objects to implement
a well-defined service interface of your own.
• IOC containers support eager instantiation and lazy loading of
services. Containers also provide support for instantiation of
managed objects, cyclical dependencies, life cycles
management, and dependency resolution between managed
objects etc.
4. What is Spring ?
Spring is an open source framework created to address the complexity
of enterprise application development. One of the chief advantages of

12
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.

5. What are the advantages of Spring framework?


The advantages of Spring are as follows:

• Spring has layered architecture. Use what you need and leave you don't need now.
• Spring Enables POJO Programming. There is no behind the scene magic here. POJO
programming enables continuous integration and testability.
• Dependency Injection and Inversion of Control Simplifies JDBC
• Open source and no vendor lock-in.

6. What are features of Spring ?

• Lightweight:

spring is lightweight when it comes to size and transparency. The basic version of
spring framework is around 1MB. And the processing overhead is also very negligible.

• Inversion of control (IOC):

Loose coupling is achieved in spring using the technique Inversion of Control. The
objects give their dependencies instead of creating or looking for dependent objects.

• Aspect oriented (AOP):

Spring supports Aspect oriented programming and enables cohesive development by


separating application business logic from system services.

• Container:

Spring contains and manages the life cycle and configuration of application objects.

• MVC Framework:

Spring comes with MVC web application framework, built on core Spring functionality.
This framework is highly configurable via strategy interfaces, and accommodates
multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other
frameworks can be easily used instead of Spring MVC Framework.

• Transaction Management:

Spring framework provides a generic abstraction layer for transaction management.


This allowing the developer to add the pluggable transaction managers, and making it
easy to demarcate transactions without dealing with low-level issues. Spring's
transaction support is not tied to J2EE environments and it can be also used in
container less environments.

• JDBC Exception Handling:

13
The JDBC abstraction layer of the Spring offers a (Roll over to view the Image )
meaningful exception hierarchy, which simplifies
the error handling strategy. Integration with
Hibernate, JDO, and iBATIS: Spring provides best
Integration services with Hibernate, JDO and
iBATIS
7. How many modules are there in Spring? What are
they?
Spring comprises of seven modules. They are..

• 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 to 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 to
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

14
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.

8. What are the types of Dependency Injection Spring supports?>

• Setter Injection:

Setter-based DI is realized by calling setter methods on your beans after invoking a no-
argument constructor or no-argument static factory method to instantiate your bean.

• Constructor Injection:

Constructor-based DI is realized by invoking a constructor with a number of arguments,


each representing a collaborator.

9. What is Bean Factory ?


A BeanFactory is like a factory class that contains a collection of beans. The BeanFactory holds
Bean Definitions of multiple beans within itself and then instantiates the bean whenever asked
for by clients.

• BeanFactory is able to create associations between collaborating objects as they are


instantiated. This removes the burden of configuration from bean itself and the beans
client.
• BeanFactory also takes part in the life cycle of a bean, making calls to custom
initialization and destruction methods.

10. What is Application Context?


A bean factory is fine to simple applications, but to take advantage of the full power of the
Spring framework, you may want to move up to Springs more advanced container, the
application context. On the surface, an application context is same as a bean factory.Both load
bean definitions, wire beans together, and dispense beans upon request. But it also provides:

• A means for resolving text messages, including support for internationalization.


• A generic way to load file resources.
• Events to beans that are registered as listeners.

11. What is the difference between Bean Factory and Application Context ?
On the surface, an application context is same as a bean factory. But application context offers
much more..

15
• Application contexts provide a means for resolving text messages, including support for
i18n of those messages.
• Application contexts provide a generic way to load file resources, such as images.
• Application contexts can publish events to beans that are registered as listeners.
• Certain operations on the container or beans in the container, which have to be
handled in a programmatic fashion with a bean factory, can be handled declaratively in
an application context.
• ResourceLoader support: Spring’s Resource interface us a flexible generic abstraction
for handling low-level resources. An application context itself is a ResourceLoader,
Hence provides an application with access to deployment-specific Resource instances.
• MessageSource support: The application context implements MessageSource, an
interface used to obtain localized messages, with the actual implementation being
pluggable

12. What are the common implementations of the Application Context ?


The three commonly used implementation of 'Application Context' are

• ClassPathXmlApplicationContext : It Loads context definition from an XML file located


in the classpath, treating context definitions as classpath resources. The application
context is loaded from the application's classpath by using the code .
ApplicationContext context = new
ClassPathXmlApplicationContext("bean.xml");
• FileSystemXmlApplicationContext : It loads context definition from an XML file in the
filesystem. The application context is loaded from the file system by using the code .
ApplicationContext context = new
FileSystemXmlApplicationContext("bean.xml");
• XmlWebApplicationContext : It loads context definition from an XML file contained
within a web application.

13. How is a typical spring implementation look like ?


For a typical Spring Application we need the following files:
• An interface that defines the functions.
• An Implementation that contains properties, its setter and getter methods, functions
etc.,
• Spring AOP (Aspect Oriented Programming)
• A XML file called Spring configuration file.
• Client program that uses the function.
14. What is the typical Bean life cycle in Spring Bean Factory Container ?
Bean life cycle in Spring Bean Factory Container is as follows:
• The spring container finds the bean’s definition from the XML file and instantiates the
bean.
• Using the dependency injection, spring populates all of the properties as specified in
the bean definition
• If the bean implements the BeanNameAware interface, the factory calls
setBeanName() passing the bean’s ID.
• If the bean implements the BeanFactoryAware interface, the factory calls
setBeanFactory(), passing an instance of itself.
• If there are any BeanPostProcessors associated with the bean, their post-
ProcessBeforeInitialization() methods will be called.

16
• If an init-method is specified for the bean, it will be called.
• Finally, if there are any BeanPostProcessors associated with the bean, their
postProcessAfterInitialization() methods will be called.

15. What do you mean by Bean wiring ?


The act of creating associations between application components (beans) within the Spring
container is reffered to as Bean wiring.
16. What do you mean by Auto Wiring?
The Spring container is able to autowire relationships between collaborating beans. This
means that it is possible to automatically let Spring resolve collaborators (other beans) for your
bean by inspecting the contents of the BeanFactory. The autowiring functionality has five
modes.
• no
• byName
• byType
• constructor
• autodirect
17. What is DelegatingVariableResolver?
Spring provides a custom JavaServer Faces VariableResolver implementation that extends
the standard Java Server Faces managed beans mechanism which lets you use JSF and Spring
together. This variable resolver is called as DelegatingVariableResolver

18. How to integrate Java Server Faces (JSF) with Spring?


JSF and Spring do share some of the same features, most noticeably in the area of IOC
services. By declaring JSF managed-beans in the faces-config.xml configuration file, you allow
the FacesServlet to instantiate that bean at startup. Your JSF pages have access to these beans
and all of their properties.We can integrate JSF and Spring in two ways:

• DelegatingVariableResolver: Spring comes with a JSF variable resolver that lets you
use JSF and Spring together.
• <?xml version="1.0" encoding="UTF-8"?>

• <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

• "http://www.springframework.org/dtd/spring-beans.dtd">


• <faces-config>

• <application>

• <variable-resolver>

• org.springframework.web.jsf.DelegatingVariableResolver

• </variable-resolver>

• </application>

• </faces-config>

17
• The DelegatingVariableResolver will first delegate value lookups to the default resolver
of the underlying JSF implementation, and then to Spring's 'business context'
WebApplicationContext. This allows one to easily inject dependencies into one's JSF-
managed beans.
• FacesContextUtils:custom VariableResolver works well when mapping one's properties
to beans in faces-config.xml, but at times one may need to grab a bean explicitly. The
FacesContextUtils class makes this easy. It is similar to WebApplicationContextUtils,
except that it takes a FacesContext parameter rather than a ServletContext parameter.
• ApplicationContext ctx =
FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstanc
e());

19. What is Java Server Faces (JSF) - Spring integration mechanism?


Spring provides a custom JavaServer Faces VariableResolver implementation that extends the
standard JavaServer Faces managed beans mechanism. When asked to resolve a variable •
name, the following algorithm is performed:

• Does a bean with the specified name already exist in some scope (request, session,
application)? If so, return it
• Is there a standard JavaServer Faces managed bean definition for this variable name? If
so, invoke it in the usual way, and return the bean that was created.
• Is there configuration information for this variable name in the Spring
WebApplicationContext for this application? If so, use it to create and configure an
instance, and return that instance to the caller.
• If there is no managed bean or Spring definition for this variable name, return null
instead.
• BeanFactory also takes part in the life cycle of a bean, making calls to custom
initialization and destruction methods.

As a result of this algorithm, you can transparently use either JavaServer Faces or
Spring facilities to create beans on demand.

20. What is Significance of JSF- Spring integration ?

18
Spring - JSF integration is useful when an event handler wishes to explicitly invoke the bean
factory to create beans on demand, such as a bean that encapsulates the business logic to be
performed when a submit button is pressed.

21. How to integrate your Struts application with Spring?


To integrate your Struts application with Spring, we have two options:

• Configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and
set their dependencies in a Spring context file.
• Subclass Spring's ActionSupport classes and grab your Spring-managed beans explicitly
using a getWebApplicationContext() method.

22. What are ORM’s Spring supports ?


Spring supports the following ORM’s
:

• Hibernate
• iBatis
• JPA (Java Persistence API)
• TopLink
• JDO (Java Data Objects)
• OJB

23. What are the ways to access Hibernate using Spring ?


There are two approaches to Spring’s Hibernate integration:

• Inversion of Control with a HibernateTemplate and Callback


• Extending HibernateDaoSupport and Applying an AOP Interceptor

24. How to integrate Spring and Hibernate using HibernateDaoSupport?


Spring and Hibernate can integrate using Spring’s SessionFactory called LocalSessionFactory.
The integration process is of 3 steps.

• Configure the Hibernate SessionFactory


• Extend your DAO Implementation from HibernateDaoSupport
• Wire in Transaction Support with AOP

25. What are Bean scopes in Spring Framework ?


The Spring Framework supports exactly five scopes (of which three are available only if you
are using a web-aware ApplicationContext). The scopes supported are listed below:

Scope Description

Scopes a single bean definition to a single object instance per Spring IoC
singleton
container.

prototype Scopes a single bean definition to any number of object instances.

19
Scope Description

Scopes a single bean definition to the lifecycle of a single HTTP request; that is
each and every HTTP request will have its own instance of a bean created off
request
the back of a single bean definition. Only valid in the context of a web-aware
Spring ApplicationContext.

Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid
session
in the context of a web-aware Spring ApplicationContext.

Scopes a single bean definition to the lifecycle of a global HTTP Session.


global
Typically only valid when used in a portlet context. Only valid in the context of
session Don't Miss...
a web-aware Spring ApplicationContext.
Spring Certification
article
Spring Basic Tutorial
26. What is AOP?
JSF Basic Tutorial
Aspect-oriented programming, or AOP, is a programming technique
JSF-Spring2.0 Integration
that allows programmers to modularize crosscutting concerns, or
behavior that cuts across the typical divisions of responsibility, such as Spring-iBatis Integration
logging and transaction management. The core construct of AOP is the
aspect, which encapsulates behaviors affecting multiple classes into
reusable modules.

27. How the AOP used in Spring?


AOP is used in the Spring
Framework: To provide declarative
enterprise services, especially as a People who read this, also read:-
replacement for EJB declarative
services. The most important such • Webservices Interview Questions
service is declarative transaction • webMethods Questions
management, which builds on the
Spring Framework's transaction • iBatis Tutorial
abstraction.To allow users to • Let Spring Manage JSF Beans
implement custom aspects,
complementing their use of OOP • JSP Interview Questions
with AOP.

28. What do you mean by Aspect ?


A modularization of a concern that cuts across multiple objects. Transaction management is
a good example of a crosscutting concern in J2EE applications. In Spring AOP, aspects are
implemented using regular classes (the schema-based approach) or regular classes annotated
with the @Aspect annotation (@AspectJ style).

29. What do you mean by JointPoint?


A point during the execution of a program, such as the execution of a method or the handling
of an exception. In Spring AOP, a join point always represents a method execution.

30. What do you mean by Advice?

20
Action taken by an aspect at a particular join point. Different types of advice include "around,"
"before" and "after" advice. Many AOP frameworks, including Spring, model an advice as an
interceptor, maintaining a chain of interceptors "around" the join point.

31. What are the types of Advice?


Types of advice:

• Before advice: Advice that executes before a join point, but which does not have the
ability to prevent execution flow proceeding to the join point (unless it throws an
exception).
• After returning advice: Advice to be executed after a join point completes normally:
for example, if a method returns without throwing an exception.
• After throwing advice: Advice to be executed if a method exits by throwing an
exception.
• After (finally) advice: Advice to be executed regardless of the means by which a join
point exits (normal or exceptional return).
• Around advice: Advice that surrounds a join point such as a method invocation. This is
the most powerful kind of advice. Around advice can perform custom behavior before
and after the method invocation. It is also responsible for choosing whether to proceed
to the join point or to shortcut the advised method execution by returning its own
return value or throwing an exception

32. What are the types of the transaction management Spring supports ?
Spring Framework supports:

• Programmatic transaction management.


• Declarative transaction management.

33. What are the benefits of the Spring Framework transaction management ?
The Spring Framework provides a consistent abstraction for transaction management that
delivers the following benefits:

• Provides a consistent programming model across different transaction APIs such as JTA,
JDBC, Hibernate, JPA, and JDO.
• Supports declarative transaction management.
• Provides a simpler API for programmatic transaction management than a number of
complex transaction APIs such as JTA.
• Integrates very well with Spring's various data access abstractions.

34. Why most users of the Spring Framework choose declarative transaction management ?
Most users of the Spring Framework choose declarative transaction management because it is
the option with the least impact on application code, and hence is most consistent with the
ideals of a non-invasive lightweight container.

35. Explain the similarities and differences between EJB CMT and the Spring Framework's
declarative transaction
management ?

21
The basic approach is similar: it is possible to specify transaction behavior (or lack of it)
down to individual method level. It is
possible to make a setRollbackOnly() call within a transaction
context if necessary. The differences are:

• Unlike EJB CMT, which is tied to JTA, the Spring Framework's


declarative transaction management works in any
environment. It can work with JDBC, JDO, Hibernate or other
transactions under the covers, with configuration changes
only.
• The Spring Framework enables declarative transaction
management to be applied to any class, not merely special classes such as EJBs.
• The Spring Framework offers declarative rollback rules: this is a feature with no EJB
equivalent. Both programmatic and declarative support for rollback rules is provided.
• The Spring Framework gives you an opportunity to customize transactional behavior,
using AOP. With EJB CMT, you have no way to influence the container's transaction
management other than setRollbackOnly().
• The Spring Framework does not support propagation of transaction contexts across
remote calls, as do high-end application servers.

37. When to use programmatic and declarative transaction management ?


Programmatic transaction management is usually a good idea •only if you have a small
number of transactional operations.
On the other hand, if your application has numerous transactional operations, declarative
transaction management is usually worthwhile. It keeps transaction management out of
business logic, and is not difficult to configure.

38. Explain about the Spring DAO support ?


The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data
access technologies like JDBC, Hibernate or JDO in a consistent way. This allows one to switch
between the persistence technologies fairly easily and it also allows one to code without
worrying about catching exceptions that are specific to each technology.

39. What are the exceptions thrown by the Spring DAO classes ?
Spring DAO classes throw exceptions which are subclasses of
DataAccessException(org.springframework.dao.DataAccessException).Spring provides a
convenient translation from technology-specific exceptions like SQLException to its own
exception class hierarchy with the DataAccessException as the root exception. These
exceptions wrap the original exception.

40. What is SQLExceptionTranslator ?


SQLExceptionTranslator, is an interface to be implemented by classes that can translate
between SQLExceptions and Spring's own data-access-strategy-agnostic
org.springframework.dao.DataAccessException

22
41. What is Spring's JdbcTemplate ?
Spring's JdbcTemplate is central class to interact with a database through JDBC. JdbcTemplate
provides many convenience methods for doing things such as converting database data into
primitives or objects, executing prepared and callable statements, and providing custom
database error handling.
JdbcTemplate template = new JdbcTemplate

43. What is SQLProvider ?


SQLProvider:

• Has one method – getSql()


• Typically implemented by PreparedStatementCreator implementers.
• Useful for debugging. o

44. What is RowCallbackHandler ?


The RowCallbackHandler interface extracts values from each row of a ResultSet.

• Has one method – processRow(ResultSet)


• Called for each row in ResultSet.
• Typically stateful.

45. What are the differences between EJB and Spring ?


Spring and EJB feature comparison.
Feature EJB Spring
Transaction • Must use a JTA • Supports multiple transaction
management transaction manager. environments through its
PlatformTransactionManager
• Supports transactions interface, including JTA, Hibernate,
that span remote JDO, and JDBC.
method calls.
• Does not natively support distributed
transactions—it must be used with a
JTA transaction manager.
Declarative • Can define • Can define transactions declaratively
transaction transactions through the Spring configuration file
support declaratively through or through class metadata.
the deployment • Can define which methods to apply
descriptor. transaction behavior explicitly or by
• Can define transaction using regular expressions.
behavior per method
or per class by using • Can declaratively define rollback
the wildcard character behavior per method and per
*. exception type.

• Cannot declaratively

23
define rollback
behavior—this must be
done
programmatically.
Persistence Supports programmatic bean- Provides a framework for integrating with
managed persistence and several persistence technologies, including
declarative container JDBC, Hibernate, JDO, and iBATIS.
managed persistence.
Declarative • Supports declarative • No security implementation out-of-
security security through users the box.
and roles. The
management and • Acegi, an open source security
implementation of framework built on top of Spring,
users and roles is provides declarative security through
container specific. the Spring configuration file or class
metadata.
• Declarative security is
configured in the
deployment
descriptor.
Distributed Provides container-managed Provides proxying for remote calls via RMI,
computing remote method calls. JAX-RPC, and web services.

Hibernate
1.What is ORM ?
ORM stands for object/relational mapping. ORM is the automated persistence of objects in a
Java application to the tables in a relational database.

2.What does ORM consists of ?


An ORM solution consists of the followig four pieces:

• API for performing basic CRUD operations


• API to express queries refering to classes
• Facilities to specify metadata
• Optimization facilities : dirty checking,lazy associations fetching

3.What are the ORM levels ?


The ORM levels are:

• Pure relational (stored procedure.)


• Light objects mapping (JDBC)
• Medium object mapping
• Full object Mapping (composition,inheritance, polymorphism, persistence by
reachability)

4.What is Hibernate?

24
Hibernate is a pure Java object-relational mapping (ORM) and persistence framework
that allows you to map plain old Java objects to relational database tables using (XML)
configuration files.Its purpose is to relieve the developer from a significant amount of
relational data persistence-related programming tasks.

5.Why do you need ORM tools like hibernate?


The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart
from this, ORM provides following benefits:

• Improved productivity
o High-level object-oriented API
o Less Java code to write
o No SQL to write
• Improved performance
o Sophisticated caching
o Lazy loading
o Eager loading
• Improved maintainability
o A lot less code to write
• Improved portability
o ORM framework generates database-specific SQL for you

6.What Does Hibernate Simplify?


Hibernate simplifies:

• Saving and retrieving your domain objects


• Making database column and table name changes
• Centralizing pre save and post retrieve logic
• Complex joins for retrieving related items
• Schema creation from object model

7.What is the need for Hibernate xml mapping file?


Hibernate mapping file tells Hibernate which tables and columns to use to load and store
objects. Typical mapping file look as follows:

25
8.What are the most common methods of Hibernate configuration?
The most common methods of Hibernate configuration are:

• Programmatic configuration
• XML configuration (hibernate.cfg.xml)

9.What are the important tags of hibernate.cfg.xml?


Following are the important tags of hibernate.cfg.xml:

10.What are the Core interfaces are of Hibernate framework?

26
The five core interfaces are used in just about every Hibernate application. Using these
interfaces, you can store and retrieve persistent objects and control transactions.

• Session interface
• SessionFactory interface
• Configuration interface
• Transaction interface
• Query and Criteria interfaces

11.What role does the Session interface play in Hibernate?


The Session interface is the primary interface used by Hibernate applications. It is a single-
threaded, short-lived object representing a conversation between the application and the
persistent store. It allows you to create query objects to retrieve persistent objects.

Session session = sessionFactory.openSession();


Session interface role:

• Wraps a JDBC connection


• Factory for Transaction
• Holds a mandatory (first-level) cache of persistent objects, used when navigating the
object graph or looking up objects by identifier

12.What role does the SessionFactory interface play in Hibernate?


The application obtains Session instances from a SessionFactory. There is typically a single
SessionFactory for the whole application—created during application initialization. The
SessionFactory caches generate SQL statements and other mapping metadata that Hibernate
uses at runtime. It also holds cached data that has been read in one unit of work and may be
reused in a future unit of work

SessionFactory sessionFactory = configuration.buildSessionFactory();

13.What is the general flow of Hibernate communication with RDBMS?


The general flow of Hibernate communication with RDBMS is :

• Load the Hibernate configuration file and create configuration object. It will
automatically load all hbm mapping files
• Create session factory from configuration object
• Get one session from this session factory
• Create HQL Query
• Execute query to get list containing Java objects

14.What is Hibernate Query Language (HQL)?


Hibernate offers a query language that embodies a very powerful and flexible mechanism to
query, store, update, and retrieve objects from a database. This language, the Hibernate query
Language (HQL), is an object-oriented extension to SQL.

15.How do you map Java Objects with Database tables?

27
• First we need to write Java domain objects (beans with setter and getter).
• Write hbm.xml, where we map java class to table and database columns to Java class
variables.

Example :
<hibernate-mapping>
<class name="com.test.User" table="user">
<property column="USER_NAME" length="255"
name="userName" not-null="true" type="java.lang.String"/>
<property column="USER_PASSWORD" length="255"
name="userPassword" not-null="true"
type="java.lang.String"/>
</class>
</hibernate-mapping>

16.What’s the difference between load() and get()?


load() vs. get() :-
load() get()

Only use the load() method if you are sure If you are not sure that the object exists,
that the object exists. then use one of the get() methods.

load() method will throw an exception if the get() method will return null if the
unique id is not found in the database. unique id is not found in the database.

load() just returns a proxy by default and


database won’t be hit until the proxy is first get() will hit the database immediately.
invoked.

17.What is the difference between and merge and update ?


Use update() if you are sure that the session does not contain an already persistent instance
with the same identifier, and merge() if you want to merge your modifications at any time
without consideration of the state of the session.

18.How do you define sequence generated primary key in hibernate?


Using <generator> tag.
Example:-
<id column="USER_ID" name="id" type="java.lang.Long">
<generator class="sequence">
<param name="table">SEQUENCE_NAME</param>
<generator>
</id>

19.Define cascade and inverse option in one-many mapping?

28
cascade - enable operations to cascade to child entities.
cascade="all|none|save-update|delete|all-delete-orphan"

inverse - mark this collection as the "inverse" end of a bidirectional association.


inverse="true|false"
Essentially "inverse" indicates which end of a relationship should be ignored, so when persisting
a parent who has a collection of children, should you ask the parent for its list of children, or
ask the children who the parents are?

20.What do you mean by Named – SQL query?


Named SQL queries are defined in the mapping xml document and called wherever required.
Example:
<sql-query name = "empdetails">
<return alias="emp" class="com.test.Employee"/>
SELECT emp.EMP_ID AS {emp.empid},
emp.EMP_ADDRESS AS {emp.address},
emp.EMP_NAME AS {emp.name}
FROM Employee EMP WHERE emp.NAME LIKE :name
</sql-query>

Invoke Named Query :


List people = session.getNamedQuery("empdetails")
.setString("TomBrady", name)
.setMaxResults(50)
.list();

21.How do you invoke Stored Procedures?

<sql-query name="selectAllEmployees_SP" callable="true">


<return alias="emp" class="employee">
<return-property name="empid" column="EMP_ID"/>

<return-property name="name" column="EMP_NAME"/>


<return-property name="address" column="EMP_ADDRESS"/>
{ ? = call selectAllEmployees() }
</return>
</sql-query>

29
22.Explain Criteria API
Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very
convenient approach for functionality like "search" screens where there is a variable number of
conditions to be placed upon the result set.
Example :
List employees = session.createCriteria(Employee.class)
.add(Restrictions.like("name", "a%") )
.add(Restrictions.like("address",
"Boston"))
.addOrder(Order.asc("name") )
.list();

23.Define HibernateTemplate?
org.springframework.orm.hibernate.HibernateTemplate is a helper class which provides
different methods for querying/retrieving data from the database. It also converts checked
HibernateExceptions into unchecked DataAccessExceptions.

24.What are the benefits does HibernateTemplate provide?


The benefits of HibernateTemplate are :

• HibernateTemplate, a Spring Template class simplifies interactions with Hibernate


Session.
• Common functions are simplified to single method calls.
• Sessions are automatically closed.
• Exceptions are automatically caught and converted to runtime exceptions.

25.How do you switch between relational databases without code changes?


Using Hibernate SQL Dialects , we can switch databases. Hibernate will generate appropriate
hql queries based on the dialect defined.

26.If you want to see the Hibernate generated SQL statements on console, what should we
do?
In Hibernate configuration file set as follows:
<property name="show_sql">true</property>

27.What are derived properties?


The properties that are not mapped to a column, but calculated at runtime by evaluation of an
expression are called derived properties. The expression can be defined using the formula
attribute of the element.


28.What is component mapping in Hibernate?

• A component is an object saved as a value, not as a reference


• A component can be saved directly without needing to declare interfaces or identifier
properties

30
• Required to define an empty constructor
• Shared references not supported

Example:

29.What is the difference between sorted and ordered collection in hibernate?


sorted collection vs. order collection :-
sorted collection order collection

A sorted collection is sorting a collection by


utilizing the sorting features provided by the
Order collection is sorting a collection by
Java collections framework. The sorting
specifying the order-by clause for sorting
occurs in the memory of JVM which running
this collection when retrieval.
Hibernate, after the data being read from
database using java comparator.

If your collection is not large, it will be more If your collection is very large, it will be
efficient way to sort it. more efficient way to sort it .

31.What is the advantage of Hibernate over jdbc?


Hibernate Vs. JDBC :-
JDBC Hibernate

With JDBC, developer has to write code to Hibernate is flexible and powerful ORM
map an object model's data representation solution to map Java classes to database

31
tables. Hibernate itself takes care of this
to a relational data model and its
mapping using XML files so developer does
corresponding database schema.
not need to write code for this.

Hibernate provides transparent persistence


With JDBC, the automatic mapping of Java
and developer does not need to write code
objects with database tables and vice versa
explicitly to map database tables tuples to
conversion is to be taken care of by the
application objects during interaction with
developer manually with lines of code.
RDBMS.

Hibernate provides a powerful query


language Hibernate Query Language
JDBC supports only native Structured Query (independent from type of database) that is
Language (SQL). Developer has to find out expressed in a familiar SQL like syntax and
the efficient way to access database, i.e. to includes full support for polymorphic
select effective query from a number of queries. Hibernate also supports native SQL
queries to perform same task. statements. It also selects an effective way
to perform a database manipulation task for
an application.

Application using JDBC to handle persistent


data (database tables) having database
specific code in large amount. The code Hibernate provides this mapping itself. The
written to map table data to application actual mapping between tables and
objects and vice versa is actually to map application objects is done in XML files. If
table fields to object properties. As table there is change in Database or in any table
changed or database changed then it’s then the only need to change XML file
essential to change object structure as well properties.
as to change code written to map table-to-
object/object-to-table.

Hibernate reduces lines of code by


With JDBC, it is developer’s responsibility to
maintaining object-table mapping itself and
handle JDBC result set and convert it to Java
returns result to application in form of Java
objects through code to use this persistent
objects. It relieves programmer from manual
data in application. So with JDBC, mapping
handling of persistent data, hence reducing
between Java objects and database tables is
the development time and maintenance
done manually.
cost.

Hibernate, with Transparent Persistence,


cache is set to application work space.
Relational tuples are moved to this cache as
a result of query. It improves performance if
With JDBC, caching is maintained by hand-
client application reads same data many
coding.
times for same write. Automatic Transparent
Persistence allows the developer to
concentrate more on business logic rather
than this application code.

In JDBC there is no check that always every Hibernate enables developer to define
user has updated data. This check has to be version type field to application, due to this
added by the developer. defined field Hibernate updates version field
of database table every time relational tuple
is updated in form of Java class object to
that table. So if two users retrieve same
tuple and then modify it and one user save

32
this modified tuple to database, version is
automatically updated for this tuple by
Hibernate. When other user tries to save
updated tuple to database then it does not
allow saving it because this user does not
have updated data.

32.What are the Collection types in Hibernate ?

• Bag
• Set
• List
• Array
• Map

33.What are the ways to express joins in HQL?


HQL provides four ways of expressing (inner and outer) joins:-

• An implicit association join


• An ordinary join in the FROM clause
• A fetch join in the FROM clause.
• A theta-style join in the WHERE clause.

34.Define cascade and inverse option in one-many mapping?


cascade - enable operations to cascade to child entities.
cascade="all|none|save-update|delete|all-delete-orphan"

inverse - mark this collection as the "inverse" end of a bidirectional association.


inverse="true|false"
Essentially "inverse" indicates which end of a relationship should be ignored, so when persisting
a parent who has a collection of children, should you ask the parent for its list of children, or
ask the children who the parents are?

35.What is Hibernate proxy?


The proxy attribute enables lazy initialization of persistent instances of the class. Hibernate
will initially return CGLIB proxies which implement the named interface. The actual persistent
object will be loaded when a method of the proxy is invoked.

36.How can Hibernate be configured to access an instance variable directly and not
through a setter method ?
By mapping the property with access="field" in Hibernate metadata. This forces hibernate to
bypass the setter method and access the instance variable directly while initializing a newly
loaded object.

37.How can a whole class be mapped as immutable?

Mark the class as mutable="false" (Default is true),. This specifies that instances of the class
are (not) mutable. Immutable classes, may not be updated or deleted by the application.

33
38.What is the use of dynamic-insert and dynamic-update attributes in a class mapping?
Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very
convenient approach for functionality like "search" screens where there is a variable number of
conditions to be placed upon the result set.

• dynamic-update (defaults to false): Specifies that UPDATE SQL should be generated at


runtime and contain only those columns whose values have changed
• dynamic-insert (defaults to false): Specifies that INSERT SQL should be generated at
runtime and contain only the columns whose values are not null.

39.What do you mean by fetching strategy ?


A fetching strategy is the strategy Hibernate will use for retrieving associated objects if the
application needs to navigate the association. Fetch strategies may be declared in the O/R
mapping metadata, or over-ridden by a particular HQL or Criteria query.

40.What is automatic dirty checking?


Automatic dirty checking is a feature that saves us the effort of explicitly asking Hibernate to
update the database when we modify the state of an object inside a transaction.

41.What is transactional write-behind?


Hibernate uses a sophisticated algorithm to determine an efficient ordering that avoids
database foreign key constraint violations but is still sufficiently predictable to the user. This
feature is called transactional write-behind.


42.What are Callback interfaces?
Callback interfaces allow the application to receive a notification when something interesting
happens to an object—for example, when an object is loaded, saved, or deleted. Hibernate
applications don't need to implement these callbacks, but they're useful for implementing
certain kinds of generic functionality.

43.What are the types of Hibernate instance states ?


Three types of instance states:

• Transient -The instance is not associated with any persistence context


• Persistent -The instance is associated with a persistence context
• Detached -The instance was associated with a persistence context which has been
closed – currently not associated

44.What are the differences between EJB 3.0 & Hibernate


Hibernate Vs EJB 3.0 :-
Hibernate EJB 3.0

Persistence Context-Set of entities that


Session–Cache or collection of loaded objects
can be managed by a given EntityManager is
relating to a single unit of work
defined by a persistence unit

XDoclet Annotations used to support Java 5.0 Annotations used to support

34
Attribute Oriented Programming Attribute Oriented Programming

Defines HQL for expressing queries to the


Defines EJB QL for expressing queries
database

Supports Entity Relationships through Support Entity Relationships through Java


mapping files and annotations in JavaDoc 5.0 annotations

Provides a Persistence Manager API exposed


Provides and Entity Manager Interface for
via the Session, Query, Criteria, and
managing CRUD operations for an Entity
Transaction API

Provides callback support through lifecycle, Provides callback support through Entity
interceptor, and validatable interfaces Listener and Callback methods

Entity Relationships are unidirectional.


Entity Relationships are bidirectional or
Bidirectional relationships are implemented
unidirectional
by two unidirectional relationships

45.What are the types of inheritance models in Hibernate?


There are three types of inheritance models in Hibernate:

• Table per class hierarchy


• Table per subclass
• Table per concrete class

Q. How will you configure Hibernate?

Answer:

The configuration files hibernate.cfg.xml (or hibernate.properties) and mapping files


*.hbm.xml are used by the Configuration class to create (i.e. configure and bootstrap
hibernate) the SessionFactory, which in turn creates the Session instances. Session instances
are the primary interface for the persistence service.

" hibernate.cfg.xml (alternatively can use hibernate.properties): These two files are used to
configure the hibernate sevice (connection driver class, connection URL, connection username,
connection password, dialect etc). If both files are present in the classpath then
hibernate.cfg.xml file overrides the settings found in the hibernate.properties file.

" Mapping files (*.hbm.xml): These files are used to map persistent objects to a relational
database. It is the best practice to store each object in an individual mapping file (i.e mapping
file per class) because storing large number of persistent classes into one mapping file can be
difficult to manage and maintain. The naming convention is to use the same name as the
persistent (POJO) class name. For example Account.class will have a mapping file named
Account.hbm.xml. Alternatively hibernate annotations can be used as part of your persistent
class code instead of the *.hbm.xml files.

Q. What is a SessionFactory? Is it a thread-safe object?

Answer:

35
SessionFactory is Hibernates concept of a single datastore and is threadsafe so that many
threads can access it concurrently and request for sessions and immutable cache of compiled
mappings for a single database. A SessionFactory is usually only built once at startup.
SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed
in an application code.

SessionFactory sessionFactory = new Configuration().configure().buildSessionfactory();

Q. What is a Session? Can you share a session object between different theads?

Answer:

Session is a light weight and a non-threadsafe object (No, you cannot share it between
threads) that represents a single unit-of-work with the database. Sessions are opened by a
SessionFactory and then are closed when all work is complete. Session is the primary interface
for the persistence service. A session obtains a database connection lazily (i.e. only when
required). To avoid creating too many sessions ThreadLocal class can be used as shown below
to get the current session no matter how many times you make call to the currentSession()
method.

&
public class HibernateUtil {
&
public static final ThreadLocal local = new ThreadLocal();

public static Session currentSession() throws HibernateException {


Session session = (Session) local.get();
//open a new session if this thread has no session
if(session == null) {
session = sessionFactory.openSession();
local.set(session);
}
return session;
}
}

It is also vital that you close your session after your unit of work completes. Note: Keep your
Hibernate Session API handy.

Q. What are the benefits of detached objects?

Answer:

Detached objects can be passed across layers all the way up to the presentation layer without
having to use any DTOs (Data Transfer Objects). You can later on re-attach the detached
objects to another session.

Q. What are the pros and cons of detached objects?

Answer:

Pros:

" When long transactions are required due to user think-time, it is the best practice to break
the long transaction up into two or more transactions. You can use detached objects from the
first transaction to carry data all the way up to the presentation layer. These detached objects

36
get modified outside a transaction and later on re-attached to a new transaction via another
session.

Cons

" In general, working with detached objects is quite cumbersome, and better to not clutter up
the session with them if possible. It is better to discard them and re-fetch them on subsequent
requests. This approach is not only more portable but also more efficient because - the objects
hang around in Hibernate's cache anyway.

" Also from pure rich domain driven design perspective it is recommended to use DTOs
(DataTransferObjects) and DOs (DomainObjects) to maintain the separation between Service
and UI tiers.

Q. How does Hibernate distinguish between transient (i.e. newly instantiated) and
detached objects?

Answer

" Hibernate uses the version property, if there is one.


" If not uses the identifier value. No identifier value means a new object. This does work only
for Hibernate managed surrogate keys. Does not work for natural keys and assigned (i.e. not
managed by Hibernate) surrogate keys.
" Write your own strategy with Interceptor.isUnsaved().

Q. What is the difference between the session.get() method and the session.load() method?

Both the session.get(..) and session.load() methods create a persistent object by loading the required
object from the database. But if there was not such object in the database then the method
session.load(..) throws an exception whereas session.get(&) returns null.

Q. What is the difference between the session.update() method and the session.lock() method?

Both of these methods and saveOrUpdate() method are intended for reattaching a detached object. The
session.lock() method simply reattaches the object to the session without checking or updating the
database on the assumption that the database in sync with the detached object. It is the best practice to
use either session.update(..) or session.saveOrUpdate(). Use session.lock() only if you are absolutely sure
that the detached object is in sync with your detached object or if it does not matter because you will be
overwriting all the columns that would have changed later on within the same transaction.

Note: When you reattach detached objects you need to make sure that the dependent objects are
reatched as well.

Q. How would you reatach detached objects to a session when the same object has already
been loaded into the session?

You can use the session.merge() method call.

Q. What are the general considerations or best practices for defining your Hibernate persistent
classes?

1.You must have a default no-argument constructor for your persistent classes and there should be
getXXX() (i.e accessor/getter) and setXXX( i.e. mutator/setter) methods for all your persistable instance
variables.

2.You should implement the equals() and hashCode() methods based on your business key and it is
important not to use the id field in your equals() and hashCode() definition if the id field is a surrogate key

37
(i.e. Hibernate managed identifier). This is because the Hibernate only generates and sets the field when
saving the object.

3. It is recommended to implement the Serializable interface. This is potentially useful if you want to
migrate around a multi-processor cluster.

4.The persistent class should not be final because if it is final then lazy loading cannot be used by creating
proxy objects.

5.Use XDoclet tags for generating your *.hbm.xml files or Annotations (JDK 1.5 onwards), which are less
verbose than *.hbm.xml files.

How can I count the number of query results without actually


returning them?
Integer count = (Integer) session.createQuery("select count(*)
from ....").uniqueResult();

How can I find the size of a collection without initializing it?


Integer size = (Integer) s.createFilter( collection, "select
count(*)" ).uniqueResult();

How can I order by the size of a collection?


Use a left join, together with group by

select user
from User user
left join user.messages msg
group by user
order by count(msg)

How can I place a condition upon a collection size?


If your database supports subselects:

from User user where size(user.messages) >= 1

or:

from User user where exists elements(user.messages)

38
If not, and in the case of a one-to-many or many-to-many association:

select user
from User user
join user.messages msg
group by user
having count(msg) >= 1

Because of the inner join, this form can't be used to return a User with zero messages, so the following form is
also useful

select user
from User as user
left join user.messages as msg
group by user
having count(msg) = 0

How can I query for entities with empty collections?


from Box box
where box.balls is empty

Or, try this:

select box
from Box box
left join box.balls ball
where ball is null

How can I sort / order collection elements?


There are three different approaches:

1. Use a SortedSet or SortedMap, specifying a comparator class in the sort attribute or <set> or <map>. This
solution does a sort in memory.

2. Specify an order-by attribute of <set>, <map> or <bag>, naming a list of table columns to sort by. This
solution works only in JDK 1.4+.

3. Use a filter session.createFilter( collection, "order by ...." ).list()

Are collections pageable?

39
Query q = s.createFilter( collection, "" ); // the trivial filter
q.setMaxResults(PAGE_SIZE);
q.setFirstResult(PAGE_SIZE * pageNumber);
List page = q.list();

I have a one-to-one association between two classes. Ensuring that


associated objects have matching identifiers is bugprone. Is there
a better way?
<generator class="foreign">
<param name="property">parent</param>
</generator>

I have a many-to-many association between two tables, but the


association table has some extra columns (apart from the foreign
keys). What kind of mapping should I use?
Use a composite-element to model the association table. For example, given the following association table:

create table relationship (


fk_of_foo bigint not null,
fk_of_bar bigint not null,
multiplicity smallint,
created date )

you could use this collection mapping (inside the mapping for class Foo):

<set name="relationship">
<key column="fk_of_foo"/>
<composite-element class="Relationship">
<property name="multiplicity" type="short" not-null="true"/>
<property name="created" type="date" not-null="true"/>
<many-to-one name="bar" class="Bar" not-null="true"/>
</composite-element>
</set>

You may also use an <idbag> with a surrogate key column for the collection table. This would allow you to have
nullable columns.

An alternative approach is to simply map the association table as a normal entity class with two bidirectional one-
to-many associations.

40
In an MVC application, how can we ensure that all proxies and lazy
collections will be initialized when the view tries to access them?
One possible approach is to leave the session open (and transaction uncommitted) when forwarding to the view.
The session/transaction would be closed/committed after the view is rendered in, for example, a servlet filter
(another example would by to use the ModelLifetime.discard() callback in Maverick). One difficulty with this
approach is making sure the session/transaction is closed/rolled back if an exception occurs rendering the view.

Another approach is to simply force initialization of all needed objects using Hibernate.initialize(). This is
often more straightforward than it sounds.

How can I bind a dynamic list of values into an in query expression?


Query q = s.createQuery("from foo in class Foo where foo.id in (:id_list)");
q.setParameterList("id_list", fooIdList);
List foos = q.list();

How can I bind properties of a JavaBean to named query


parameters?
Query q = s.createQuery("from foo in class Foo where foo.name=:name and
foo.size=:size");
q.setProperties(fooBean); // fooBean has getName() and getSize()
List foos = q.list();

Can I map an inner class?


You may persist any static inner class. You should specify the class name using the standard form ie.
eg.Foo$Bar

How can I assign a default value to a property when the database


column is null?
Use a UserType.

How can I trucate String data?


Use a UserType.

How can I trim spaces from String data persisted to a CHAR column?
Use a UserType.

41
How can I convert the type of a property to/from the database
column type?
Use a UserType.

How can I get access to O/R mapping information such as table and
column names at runtime?
This information is available via the Configuration object. For example, entity mappings may be obtained using
Configuration.getClassMapping(). It is even possible to manipulate this metamodel at runtime and then
build a new SessionFactory.

How can I create an association to an entity without fetching that


entity from the database (if I know the identifier)?
If the entity is proxyable (lazy="true"), simply use load(). The following code does not result in any SELECT
statement:

Item itemProxy = (Item) session.load(Item.class, itemId);


Bid bid = new Bid(user, amount, itemProxy);
session.save(bid);

How can I retrieve the identifier of an associated object, without


fetching the association?
Just do it. The following code does not result in any SELECT statement, even if the item association is lazy.

Long itemId = bid.getItem().getId();

This works if getItem() returns a proxy and if you mapped the identifier property with regular accessor methods.
If you enabled direct field access for the id of an Item, the Item proxy will be initialized if you call getId(). This
method is then treated like any other business method of the proxy, initialization is required if it is called.

How can I manipulate mappings at runtime?


You can access (and modify) the Hibernate metamodel via the Configuration object, using
getClassMapping(), getCollectionMapping(), etc.

Note that the SessionFactory is immutable and does not retain any reference to the Configuration instance,
so you must re-build it if you wish to activate the modified mappings.

How can I avoid n+1 SQL SELECT queries when running a Hibernate
query?

42
Follow the best practices guide! Ensure that all <class> and <collection> mappings specify lazy="true" in
Hibernate2 (this is the new default in Hibernate3). Use HQL LEFT JOIN FETCH to specify which associations you
need to be retrieved in the initial SQL SELECT.

A second way to avoid the n+1 selects problem is to use fetch="subselect" in Hibernate3.

If you are still unsure, refer to the Hibernate documentation and Hibernate in Action.

I have a collection with second-level cache enabled, and Hibernate


retrieves the collection elements one at a time with a SQL query
per element!
Enable second-level cache for the associated entity class. Don't cache collections of uncached entity types.

How can I insert XML data into Oracle using the xmltype() function?
Specify custom SQL INSERT (and UPDATE) statements using <sql-insert> and <sql-update> in Hibernate3,
or using a custom persister in Hibernate 2.1.

You will also need to write a UserType to perform binding to/from the PreparedStatement.

How can I execute arbitrary SQL using Hibernate?


PreparedStatement ps = session.connection().prepareStatement(sqlString);

Or, if you wish to retrieve managed entity objects, use session.createSQLQuery().

Or, in Hibernate3, override generated SQL using <sql-insert>, <sql-update>, <sql-delete> and
<loader> in the mapping document.

I want to call an SQL function from HQL, but the HQL parser does
not recognize it!
Subclass your Dialect, and call registerFunction() from the constructor.

How can I count the number of query results without actually


returning them?
Integer count = (Integer) session.createQuery("select count(*)
from ....").uniqueResult();

How can I find the size of a collection without initializing it?

43
Integer size = (Integer) s.createFilter( collection, "select
count(*)" ).uniqueResult();

How can I order by the size of a collection?


Use a left join, together with group by

select user
from User user
left join user.messages msg
group by user
order by count(msg)

How can I place a condition upon a collection size?


If your database supports subselects:

from User user where size(user.messages) >= 1

or:

from User user where exists elements(user.messages)

If not, and in the case of a one-to-many or many-to-many association:

select user
from User user
join user.messages msg
group by user
having count(msg) >= 1

Because of the inner join, this form can't be used to return a User with zero messages, so the following form is
also useful

select user
from User as user
left join user.messages as msg
group by user
having count(msg) = 0

How can I query for entities with empty collections?


from Box box

44
where box.balls is empty

Or, try this:

select box
from Box box
left join box.balls ball
where ball is null

How can I sort / order collection elements?


There are three different approaches:

1. Use a SortedSet or SortedMap, specifying a comparator class in the sort attribute or <set> or <map>. This
solution does a sort in memory.

2. Specify an order-by attribute of <set>, <map> or <bag>, naming a list of table columns to sort by. This
solution works only in JDK 1.4+.

3. Use a filter session.createFilter( collection, "order by ...." ).list()

Are collections pageable?


Query q = s.createFilter( collection, "" ); // the trivial filter
q.setMaxResults(PAGE_SIZE);
q.setFirstResult(PAGE_SIZE * pageNumber);
List page = q.list();

I have a one-to-one association between two classes. Ensuring that


associated objects have matching identifiers is bugprone. Is there
a better way?
<generator class="foreign">
<param name="property">parent</param>
</generator>

I have a many-to-many association between two tables, but the


association table has some extra columns (apart from the foreign
keys). What kind of mapping should I use?
Use a composite-element to model the association table. For example, given the following association table:

45
create table relationship (
fk_of_foo bigint not null,
fk_of_bar bigint not null,
multiplicity smallint,
created date )

you could use this collection mapping (inside the mapping for class Foo):

<set name="relationship">
<key column="fk_of_foo"/>
<composite-element class="Relationship">
<property name="multiplicity" type="short" not-null="true"/>
<property name="created" type="date" not-null="true"/>
<many-to-one name="bar" class="Bar" not-null="true"/>
</composite-element>
</set>

You may also use an <idbag> with a surrogate key column for the collection table. This would allow you to have
nullable columns.

An alternative approach is to simply map the association table as a normal entity class with two bidirectional one-
to-many associations.

In an MVC application, how can we ensure that all proxies and lazy
collections will be initialized when the view tries to access them?
One possible approach is to leave the session open (and transaction uncommitted) when forwarding to the view.
The session/transaction would be closed/committed after the view is rendered in, for example, a servlet filter
(another example would by to use the ModelLifetime.discard() callback in Maverick). One difficulty with this
approach is making sure the session/transaction is closed/rolled back if an exception occurs rendering the view.

Another approach is to simply force initialization of all needed objects using Hibernate.initialize(). This is
often more straightforward than it sounds.

How can I bind a dynamic list of values into an in query expression?


Query q = s.createQuery("from foo in class Foo where foo.id in (:id_list)");
q.setParameterList("id_list", fooIdList);
List foos = q.list();

How can I bind properties of a JavaBean to named query


parameters?

46
Query q = s.createQuery("from foo in class Foo where foo.name=:name and
foo.size=:size");
q.setProperties(fooBean); // fooBean has getName() and getSize()
List foos = q.list();

Can I map an inner class?


You may persist any static inner class. You should specify the class name using the standard form ie.
eg.Foo$Bar

How can I assign a default value to a property when the database


column is null?
Use a UserType.

How can I trucate String data?


Use a UserType.

How can I trim spaces from String data persisted to a CHAR column?
Use a UserType.

How can I convert the type of a property to/from the database


column type?
Use a UserType.

How can I get access to O/R mapping information such as table and
column names at runtime?
This information is available via the Configuration object. For example, entity mappings may be obtained using
Configuration.getClassMapping(). It is even possible to manipulate this metamodel at runtime and then
build a new SessionFactory.

How can I create an association to an entity without fetching that


entity from the database (if I know the identifier)?
If the entity is proxyable (lazy="true"), simply use load(). The following code does not result in any SELECT
statement:

Item itemProxy = (Item) session.load(Item.class, itemId);


Bid bid = new Bid(user, amount, itemProxy);

47
session.save(bid);

How can I retrieve the identifier of an associated object, without


fetching the association?
Just do it. The following code does not result in any SELECT statement, even if the item association is lazy.

Long itemId = bid.getItem().getId();

This works if getItem() returns a proxy and if you mapped the identifier property with regular accessor methods.
If you enabled direct field access for the id of an Item, the Item proxy will be initialized if you call getId(). This
method is then treated like any other business method of the proxy, initialization is required if it is called.

How can I manipulate mappings at runtime?


You can access (and modify) the Hibernate metamodel via the Configuration object, using
getClassMapping(), getCollectionMapping(), etc.

Note that the SessionFactory is immutable and does not retain any reference to the Configuration instance,
so you must re-build it if you wish to activate the modified mappings.

How can I avoid n+1 SQL SELECT queries when running a Hibernate
query?
Follow the best practices guide! Ensure that all <class> and <collection> mappings specify lazy="true" in
Hibernate2 (this is the new default in Hibernate3). Use HQL LEFT JOIN FETCH to specify which associations you
need to be retrieved in the initial SQL SELECT.

A second way to avoid the n+1 selects problem is to use fetch="subselect" in Hibernate3.

If you are still unsure, refer to the Hibernate documentation and Hibernate in Action.

I have a collection with second-level cache enabled, and Hibernate


retrieves the collection elements one at a time with a SQL query
per element!
Enable second-level cache for the associated entity class. Don't cache collections of uncached entity types.

How can I insert XML data into Oracle using the xmltype() function?
Specify custom SQL INSERT (and UPDATE) statements using <sql-insert> and <sql-update> in Hibernate3,
or using a custom persister in Hibernate 2.1.

You will also need to write a UserType to perform binding to/from the PreparedStatement.

48
How can I execute arbitrary SQL using Hibernate?
PreparedStatement ps = session.connection().prepareStatement(sqlString);

Or, if you wish to retrieve managed entity objects, use session.createSQLQuery().

Or, in Hibernate3, override generated SQL using <sql-insert>, <sql-update>, <sql-delete> and
<loader> in the mapping document.

I want to call an SQL function from HQL, but the HQL parser does
not recognize it!
Subclass your Dialect, and call registerFunction() from the constructor.

12.1. Introduction
The Spring Framework provides integration with Hibernate, JDO, Oracle TopLink,
iBATIS SQL Maps and JPA: in terms of resource management, DAO implementation
support, and transaction strategies. For example for Hibernate, there is first-class support
with lots of IoC convenience features, addressing many typical Hibernate integration
issues. All of these support packages for O/R (Object Relational) mappers comply with
Spring's generic transaction and DAO exception hierarchies. There are usually two
integration styles: either using Spring's DAO 'templates' or coding DAOs against plain
Hibernate/JDO/TopLink/etc APIs. In both cases, DAOs can be configured through
Dependency Injection and participate in Spring's resource and transaction management.

Spring adds significant support when using the O/R mapping layer of your choice to
create data access applications. First of all, you should know that once you started using
Spring's support for O/R mapping, you don't have to go all the way. No matter to what
extent, you're invited to review and leverage the Spring approach, before deciding to take
the effort and risk of building a similar infrastructure in-house. Much of the O/R mapping
support, no matter what technology you're using may be used in a library style, as
everything is designed as a set of reusable JavaBeans. Usage inside a Spring IoC
container does provide additional benefits in terms of ease of configuration and
deployment; as such, most examples in this section show configuration inside a Spring
container.

49
Some of the benefits of using the Spring Framework to create your ORM DAOs include:

• Ease of testing. Spring's IoC approach makes it easy to swap the implementations
and config locations of Hibernate SessionFactory instances, JDBC
DataSource instances, transaction managers, and mappes object
implementations (if needed). This makes it much easier to isolate and test each
piece of persistence-related code in isolation.
• Common data access exceptions. Spring can wrap exceptions from your O/R
mapping tool of choice, converting them from proprietary (potentially checked)
exceptions to a common runtime DataAccessException hierarchy. This allows
you to handle most persistence exceptions, which are non-recoverable, only in the
appropriate layers, without annoying boilerplate catches/throws, and exception
declarations. You can still trap and handle exceptions anywhere you need to.
Remember that JDBC exceptions (including DB specific dialects) are also
converted to the same hierarchy, meaning that you can perform some operations
with JDBC within a consistent programming model.
• General resource management. Spring application contexts can handle the
location and configuration of Hibernate SessionFactory instances, JDBC
DataSource instances, iBATIS SQL Maps configuration objects, and other
related resources. This makes these values easy to manage and change. Spring
offers efficient, easy and safe handling of persistence resources. For example:
related code using Hibernate generally needs to use the same Hibernate Session
for efficiency and proper transaction handling. Spring makes it easy to
transparently create and bind a Session to the current thread, either by using an
explicit 'template' wrapper class at the Java code level or by exposing a current
Session through the Hibernate SessionFactory (for DAOs based on plain
Hibernate API). Thus Spring solves many of the issues that repeatedly arise from
typical Hibernate usage, for any transaction environment (local or JTA).
• Integrated transaction management. Spring allows you to wrap your O/R
mapping code with either a declarative, AOP style method interceptor, or an
explicit 'template' wrapper class at the Java code level. In either case, transaction
semantics are handled for you, and proper transaction handling (rollback, etc) in
case of exceptions is taken care of. As discussed below, you also get the benefit of
being able to use and swap various transaction managers, without your
Hibernate/JDO related code being affected: for example, between local
transactions and JTA, with the same full services (such as declarative
transactions) available in both scenarios. As an additional benefit, JDBC-related
code can fully integrate transactionally with the code you use to do O/R mapping.
This is useful for data access that's not suitable for O/R mapping, such as batch
processing or streaming of BLOBs, which still needs to share common
transactions with ORM operations.

The PetClinic sample in the Spring distribution offers alternative DAO implementations
and application context configurations for JDBC, Hibernate, Oracle TopLink, and JPA.

50
PetClinic can therefore serve as working sample app that illustrates the use of Hibernate,
TopLink and JPA in a Spring web application. It also leverages declarative transaction
demarcation with different transaction strategies.

The JPetStore sample illustrates the use of iBATIS SQL Maps in a Spring environment.
It also features two web tier versions: one based on Spring Web MVC, one based on
Struts.

Beyond the samples shipped with Spring, there are a variety of Spring-based O/R
mapping samples provided by specific vendors: for example, the JDO implementations
JPOX (http://www.jpox.org/) and Kodo (http://www.bea.com/kodo/).

12.2. Hibernate
We will start with a coverage of Hibernate 3 in a Spring environment, using it to
demonstrate the approach that Spring takes towards integrating O/R mappers. This
section will cover many issues in detail and show different variations of DAO
implementations and transaction demarcation. Most of these patterns can be directly
translated to all other supported ORM tools. The following sections in this chapter will
then cover the other ORM technologies, showing briefer examples there.

Note: As of Spring 2.5, Spring requires Hibernate 3.1 or higher. Neither Hibernate 2.1
nor Hibernate 3.0 are supported anymore.

12.2.1. Resource management

Typical business applications are often cluttered with repetitive resource management
code. Many projects try to invent their own solutions for this issue, sometimes sacrificing
proper handling of failures for programming convenience. Spring advocates strikingly
simple solutions for proper resource handling, namely IoC via templating; for example
infrastructure classes with callback interfaces, or applying AOP interceptors. The
infrastructure cares for proper resource handling, and for appropriate conversion of
specific API exceptions to an unchecked infrastructure exception hierarchy. Spring
introduces a DAO exception hierarchy, applicable to any data access strategy. For direct
JDBC, the JdbcTemplate class mentioned in a previous section cares for connection
handling, and for proper conversion of SQLException to the
DataAccessException hierarchy, including translation of database-specific SQL

51
error codes to meaningful exception classes. It supports both JTA and JDBC transactions,
via respective Spring transaction managers.

Spring also offers Hibernate and JDO support, consisting of a HibernateTemplate /


JdoTemplate analogous to JdbcTemplate, a HibernateInterceptor /
JdoInterceptor, and a Hibernate / JDO transaction manager. The major goal is to
allow for clear application layering, with any data access and transaction technology, and
for loose coupling of application objects. No more business service dependencies on the
data access or transaction strategy, no more hard-coded resource lookups, no more hard-
to-replace singletons, no more custom service registries. One simple and consistent
approach to wiring up application objects, keeping them as reusable and free from
container dependencies as possible. All the individual data access features are usable on
their own but integrate nicely with Spring's application context concept, providing XML-
based configuration and cross-referencing of plain JavaBean instances that don't need to
be Spring-aware. In a typical Spring application, many important objects are JavaBeans:
data access templates, data access objects (that use the templates), transaction managers,
business services (that use the data access objects and transaction managers), web view
resolvers, web controllers (that use the business services),and so on.

12.2.2. SessionFactory setup in a Spring container

To avoid tying application objects to hard-coded resource lookups, Spring allows you to
define resources such as a JDBC DataSource or a Hibernate SessionFactory as
beans in the Spring container. Application objects that need to access resources just
receive references to such pre-defined instances via bean references (the DAO definition
in the next section illustrates this). The following excerpt from an XML application
context definition shows how to set up a JDBC DataSource and a Hibernate
SessionFactory on top of it:

<beans>

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-


method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>

52
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>product.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>

</beans>

Note that switching from a local Jakarta Commons DBCP BasicDataSource to a


JNDI-located DataSource (usually managed by an application server) is just a matter
of configuration:

<beans>

<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">


<property name="jndiName" value="java:comp/env/jdbc/myds"/>
</bean>

</beans>

You can also access a JNDI-located SessionFactory, using Spring's


JndiObjectFactoryBean to retrieve and expose it. However, that is typically not
common outside of an EJB context.

12.2.3. The HibernateTemplate

The basic programming model for templating looks as follows, for methods that can be
part of any custom data access object or business service. There are no restrictions on the
implementation of the surrounding object at all, it just needs to provide a Hibernate
SessionFactory. It can get the latter from anywhere, but preferably as bean
reference from a Spring IoC container - via a simple setSessionFactory(..) bean
property setter. The following snippets show a DAO definition in a Spring container,
referencing the above defined SessionFactory, and an example for a DAO method
implementation.

53
<beans>

<bean id="myProductDao" class="product.ProductDaoImpl">


<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

</beans>
public class ProductDaoImpl implements ProductDao {

private HibernateTemplate hibernateTemplate;

public void setSessionFactory(SessionFactory sessionFactory) {


this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}

public Collection loadProductsByCategory(String category) throws


DataAccessException {
return this.hibernateTemplate.find("from test.Product product where
product.category=?", category);
}
}

The HibernateTemplate class provides many methods that mirror the methods
exposed on the Hibernate Session interface, in addition to a number of convenience
methods such as the one shown above. If you need access to the Session to invoke
methods that are not exposed on the HibernateTemplate, you can always drop
down to a callback-based approach like so.

public class ProductDaoImpl implements ProductDao {

private HibernateTemplate hibernateTemplate;

public void setSessionFactory(SessionFactory sessionFactory) {


this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}

public Collection loadProductsByCategory(final String category) throws


DataAccessException {
return this.hibernateTemplate.execute(new HibernateCallback() {

public Object doInHibernate(Session session) {


Criteria criteria = session.createCriteria(Product.class);
criteria.add(Expression.eq("category", category));
criteria.setMaxResults(6);
return criteria.list();
}
};
}

54
}

A callback implementation effectively can be used for any Hibernate data access.
HibernateTemplate will ensure that Session instances are properly opened and
closed, and automatically participate in transactions. The template instances are thread-
safe and reusable, they can thus be kept as instance variables of the surrounding class.
For simple single step actions like a single find, load, saveOrUpdate, or delete call,
HibernateTemplate offers alternative convenience methods that can replace such
one line callback implementations. Furthermore, Spring provides a convenient
HibernateDaoSupport base class that provides a setSessionFactory(..)
method for receiving a SessionFactory, and getSessionFactory() and
getHibernateTemplate()for use by subclasses. In combination, this allows for
very simple DAO implementations for typical requirements:

public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {

public Collection loadProductsByCategory(String category) throws


DataAccessException {
return this.getHibernateTemplate().find(
"from test.Product product where product.category=?", category);
}
}

12.2.4. Implementing Spring-based DAOs without callbacks

As alternative to using Spring's HibernateTemplate to implement DAOs, data


access code can also be written in a more traditional fashion, without wrapping the
Hibernate access code in a callback, while still respecting and participating in Spring's
generic DataAccessException hierarchy. The HibernateDaoSupport base
class offers methods to access the current transactional Session and to convert
exceptions in such a scenario; similar methods are also available as static helpers on the
SessionFactoryUtils class. Note that such code will usually pass 'false' as the
value of the getSession(..) methods 'allowCreate' argument, to enforce
running within a transaction (which avoids the need to close the returned Session, as
its lifecycle is managed by the transaction).

public class HibernateProductDao extends HibernateDaoSupport implements ProductDao {

55
public Collection loadProductsByCategory(String category) throws
DataAccessException, MyException {
Session session = getSession(false);
try {
Query query = session.createQuery("from test.Product product where
product.category=?");
query.setString(0, category);
List result = query.list();
if (result == null) {
throw new MyException("No search results.");
}
return result;
}
catch (HibernateException ex) {
throw convertHibernateAccessException(ex);
}
}
}

The advantage of such direct Hibernate access code is that it allows any checked
application exception to be thrown within the data access code; contrast this to the
HibernateTemplate class which is restricted to throwing only unchecked exceptions
within the callback. Note that you can often defer the corresponding checks and the
throwing of application exceptions to after the callback, which still allows working with
HibernateTemplate. In general, the HibernateTemplate class' convenience
methods are simpler and more convenient for many scenarios.

12.2.5. Implementing DAOs based on plain Hibernate 3 API

Hibernate 3 provides a feature called "contextual Sessions", where Hibernate itself


manages one current Session per transaction. This is roughly equivalent to Spring's
synchronization of one Hibernate Session per transaction. A corresponding DAO
implementation looks like as follows, based on the plain Hibernate API:

public class ProductDaoImpl implements ProductDao {

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {


this.sessionFactory = sessionFactory;
}

public Collection loadProductsByCategory(String category) {


return this.sessionFactory.getCurrentSession()
.createQuery("from test.Product product where product.category=?")

56
.setParameter(0, category)
.list();
}
}

This style is very similar to what you will find in the Hibernate reference documentation
and examples, except for holding the SessionFactory in an instance variable. We
strongly recommend such an instance-based setup over the old-school static
HibernateUtil class from Hibernate's CaveatEmptor sample application. (In general,
do not keep any resources in static variables unless absolutely necessary.)

The above DAO follows the Dependency Injection pattern: it fits nicely into a Spring IoC
container, just like it would if coded against Spring's HibernateTemplate. Of
course, such a DAO can also be set up in plain Java (for example, in unit tests): simply
instantiate it and call setSessionFactory(..) with the desired factory reference.
As a Spring bean definition, it would look as follows:

<beans>

<bean id="myProductDao" class="product.ProductDaoImpl">


<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

</beans>

The main advantage of this DAO style is that it depends on Hibernate API only; no
import of any Spring class is required. This is of course appealing from a non-
invasiveness perspective, and will no doubt feel more natural to Hibernate developers.

However, the DAO throws plain HibernateException (which is unchecked, so


does not have to be declared or caught), which means that callers can only treat
exceptions as generally fatal - unless they want to depend on Hibernate's own exception
hierarchy. Catching specific causes such as an optimistic locking failure is not possible
without tieing the caller to the implementation strategy. This tradeoff might be acceptable
to applications that are strongly Hibernate-based and/or do not need any special exception
treatment.

Fortunately, Spring's LocalSessionFactoryBean supports Hibernate's


SessionFactory.getCurrentSession() method for any Spring transaction

57
strategy, returning the current Spring-managed transactional Session even with
HibernateTransactionManager. Of course, the standard behavior of that method
remains: returning the current Session associated with the ongoing JTA transaction, if
any (no matter whether driven by Spring's JtaTransactionManager, by EJB CMT,
or by JTA).

In summary: DAOs can be implemented based on the plain Hibernate 3 API, while still
being able to participate in Spring-managed transactions.

12.2.6. Programmatic transaction demarcation

Transactions can be demarcated in a higher level of the application, on top of such lower-
level data access services spanning any number of operations. There are no restrictions on
the implementation of the surrounding business service here as well, it just needs a
Spring PlatformTransactionManager. Again, the latter can come from
anywhere, but preferably as bean reference via a setTransactionManager(..)
method - just like the productDAO should be set via a setProductDao(..)
method. The following snippets show a transaction manager and a business service
definition in a Spring application context, and an example for a business method
implementation.

<beans>

<bean id="myTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<bean id="myProductService" class="product.ProductServiceImpl">


<property name="transactionManager" ref="myTxManager"/>
<property name="productDao" ref="myProductDao"/>
</bean>

</beans>
public class ProductServiceImpl implements ProductService {

private TransactionTemplate transactionTemplate;


private ProductDao productDao;

public void setTransactionManager(PlatformTransactionManager transactionManager)


{
this.transactionTemplate = new TransactionTemplate(transactionManager);
}

58
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}

public void increasePriceOfAllProductsInCategory(final String category) {


this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {

public void doInTransactionWithoutResult(TransactionStatus status) {


List productsToChange =
this.productDao.loadProductsByCategory(category);
// do the price increase...
}
}
);
}
}

12.2.7. Declarative transaction demarcation

Alternatively, one can use Spring's declarative transaction support, which essentially
enables you to replace explicit transaction demarcation API calls in your Java code with
an AOP transaction interceptor configured in a Spring container. This allows you to keep
business services free of repetitive transaction demarcation code, and allows you to focus
on adding business logic which is where the real value of your application lies.
Furthermore, transaction semantics like propagation behavior and isolation level can be
changed in a configuration file and do not affect the business service implementations.

<beans>

<bean id="myTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<bean id="myProductService"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="product.ProductService"/>
<property name="target">
<bean class="product.DefaultProductService">
<property name="productDao" ref="myProductDao"/>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>myTxInterceptor</value> <!-- the transaction interceptor (configured
elsewhere) -->

59
</list>
</property>
</bean>

</beans>
public class ProductServiceImpl implements ProductService {

private ProductDao productDao;

public void setProductDao(ProductDao productDao) {


this.productDao = productDao;
}

// notice the absence of transaction demarcation code in this method


// Spring's declarative transaction infrastructure will be demarcating
transactions on your behalf
public void increasePriceOfAllProductsInCategory(final String category) {
List productsToChange = this.productDao.loadProductsByCategory(category);
// ...
}
}

Spring's TransactionInterceptor allows any checked application exception to be


thrown with the callback code, while TransactionTemplate is restricted to
unchecked exceptions within the callback. TransactionTemplate will trigger a
rollback in case of an unchecked application exception, or if the transaction has been
marked rollback-only by the application (via TransactionStatus).
TransactionInterceptor behaves the same way by default but allows
configurable rollback policies per method.

The following higher level approach to declarative transactions doesn't use the
ProxyFactoryBean, and as such may be easier to use if you have a large number of
service objects that you wish to make transactional.

Note

You are strongly encouraged to read the section entitled Section 9.5, “Declarative transaction
management” if you have not done so already prior to continuing.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"

60
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- SessionFactory, DataSource, etc. omitted -->

<bean id="myTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<aop:config>
<aop:pointcut id="productServiceMethods" expression="execution(*
product.ProductService.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
</aop:config>

<tx:advice id="txAdvice" transaction-manager="myTxManager">


<tx:attributes>
<tx:method name="increasePrice*" propagation="REQUIRED"/>
<tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>

<bean id="myProductService" class="product.SimpleProductService">


<property name="productDao" ref="myProductDao"/>
</bean>

</beans>

12.2.8. Transaction management strategies

Both TransactionTemplate and TransactionInterceptor delegate the


actual transaction handling to a PlatformTransactionManager instance, which
can be a HibernateTransactionManager (for a single Hibernate
SessionFactory, using a ThreadLocal Session under the hood) or a
JtaTransactionManager (delegating to the JTA subsystem of the container) for
Hibernate applications. You could even use a custom
PlatformTransactionManager implementation. So switching from native
Hibernate transaction management to JTA, such as when facing distributed transaction
requirements for certain deployments of your application, is just a matter of

61
configuration. Simply replace the Hibernate transaction manager with Spring's JTA
transaction implementation. Both transaction demarcation and data access code will work
without changes, as they just use the generic transaction management APIs.

For distributed transactions across multiple Hibernate session factories, simply combine
JtaTransactionManager as a transaction strategy with multiple
LocalSessionFactoryBean definitions. Each of your DAOs then gets one specific
SessionFactory reference passed into its corresponding bean property. If all
underlying JDBC data sources are transactional container ones, a business service can
demarcate transactions across any number of DAOs and any number of session factories
without special regard, as long as it is using JtaTransactionManager as the
strategy.

<beans>

<bean id="myDataSource1" class="org.springframework.jndi.JndiObjectFactoryBean">


<property name="jndiName value="java:comp/env/jdbc/myds1"/>
</bean>

<bean id="myDataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">


<property name="jndiName" value="java:comp/env/jdbc/myds2"/>
</bean>

<bean id="mySessionFactory1"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource1"/>
<property name="mappingResources">
<list>
<value>product.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
</value>
</property>
</bean>

<bean id="mySessionFactory2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource2"/>
<property name="mappingResources">
<list>
<value>inventory.hbm.xml</value>
</list>

62
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.OracleDialect
</value>
</property>
</bean>

<bean id="myTxManager"
class="org.springframework.transaction.jta.JtaTransactionManager"/>

<bean id="myProductDao" class="product.ProductDaoImpl">


<property name="sessionFactory" ref="mySessionFactory1"/>
</bean>

<bean id="myInventoryDao" class="product.InventoryDaoImpl">


<property name="sessionFactory" ref="mySessionFactory2"/>
</bean>

<!-- this shows the Spring 1.x style of declarative transaction configuration -->
<!-- it is totally supported, 100% legal in Spring 2.x, but see also above for the
sleeker, Spring 2.0 style -->
<bean id="myProductService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean
">
<property name="transactionManager" ref="myTxManager"/>
<property name="target">
<bean class="product.ProductServiceImpl">
<property name="productDao" ref="myProductDao"/>
<property name="inventoryDao" ref="myInventoryDao"/>
</bean>
</property>
<property name="transactionAttributes">
<props>
<prop key="increasePrice*">PROPAGATION_REQUIRED</prop>
<prop key="someOtherBusinessMethod">PROPAGATION_REQUIRES_NEW</prop>
<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>

</beans>

Both HibernateTransactionManager and JtaTransactionManager allow


for proper JVM-level cache handling with Hibernate - without container-specific
transaction manager lookup or JCA connector (as long as not using EJB to initiate
transactions).

63
HibernateTransactionManager can export the JDBC Connection used by
Hibernate to plain JDBC access code, for a specific DataSource. This allows for high-
level transaction demarcation with mixed Hibernate/JDBC data access completely
without JTA, as long as you are just accessing one database!
HibernateTransactionManager will automatically expose the Hibernate
transaction as JDBC transaction if the passed-in SessionFactory has been set up
with a DataSource (through the "dataSource" property of the
LocalSessionFactoryBean class). Alternatively, the DataSource that the
transactions are supposed to be exposed for can also be specified explicitly, through the
"dataSource" property of the HibernateTransactionManager class.

64

You might also like