SlideShare a Scribd company logo
Spring-Hibernate tutorial
Spring Live (chapter 2 posted
online)
What is appfuse?
• AppFuse is an open-source Java EE web application framework. It is
designed for quick and easy start up of development, while also using open-
source Java technologies such as Spring framework, Hibernate and Struts.
AppFuse was originally created by Matt Raible, who wanted to eliminate the
"ramp up" time in building new web applications.
• AppFuse provides a project skeleton, similar to the one that's created by an
IDE when one clicks through a "new web project" wizard. AppFuse 1.x uses
Ant to create the project, as well as build/test/deploy it, whereas AppFuse
2.x uses Maven 2 for these tasks. IDE support was improved in 2.0 by
leveraging Maven plugins to generate IDE project files. AppFuse 1.x uses
XDoclet and JDK 1.4+.
• Unlike other "new project" wizards, the AppFuse wizard creates a number of
additional classes and files that implement features, but also serve as
examples for the developer. The project is pre-configured to talk to a
database, to deploy in an appserver, and to allow logging in.
• When AppFuse was first developed, it only supported Struts and Hibernate.
In version 2.x, it supports Hibernate, iBATIS or JPA as persistence
frameworks. For implementing the MVC model, AppFuse is compatible with
JSF, Spring MVC, Struts 2 or Tapestry.
Appfuse
• Features integrated into AppFuse includes the following:
• Authentication and Authorization
• Remember Me (saving your login information so you don't have to login
every time)
• /Registration
• SSL Switching
• E-Mail
• URL rewriting
• Skinnability
• Page Decoration
• Templated Layout
• File Upload
• This out-of-the-box functionality is one of the main features in AppFuse that
separates it from the other "CRUD Generation" frameworks, including Ruby
on Rails and Grails. The aforementioned framework, as well as AppFuse,
allow you to create master/detail pages from database tables or existing
model objects.
Tutorials posted with code
• This tutorial is posted at
• http://www.scribd.com/doc/7074123/Spring-Live-Matt-Raible
• A newer tutorial has been posted at
• http://www.sourcebeat.com/titles/springlive/public/Rev_12/Spr
• code online for both tutorials:
• http://
svn.sourcebeat.com/svn/sourcebeat/trunk/downloads/
Spring hibernate tutorial
The tutorial covers
Below are the ordered steps you will perform:
1. Download Struts and Spring.
2. Create project directories and an Ant build file.
3. Create a unit test for the persistence layer.
4. Configure Hibernate and Spring.
5. Create Hibernate DAO implementation.
6. Run the unit test and verify CRUD with DAO.
7. Create manager and declare transactions.
8. Create a unit test for the Struts action.
9. Create an action and model (DynaActionForm) for the web layer.
10. Run the unit test and verify CRUD with action.
11. Create JavaServer Pages (JSPs) to allow CRUD through a web browser.
12. Verify the JSPs’ functionality through your browser.
13. Add Validation use Commons Validator.
Downloads and environment variables
Download Struts and Spring1
1. Download and install the following components:
􀀗 Java Development Kit (JDK) 1.4.2 (or above)
􀀗 Tomcat 5.0+
􀀗 Ant 1.6.1+
2. Set up the following environment variables:
􀀗 JAVA_HOME
􀀗 ANT_HOME
􀀗 CATALINA_HOME
3. Add the following to your PATH environment variable:
􀀗 JAVA_HOME/bin
􀀗 ANT_HOME/bin
􀀗 CATALINA_HOME/bin
The approach
To develop a Java-based web application, developers usually download JARs, create a directory
structure, and create an Ant build file. For a Struts-only application, this can be simplified
by using the struts-blank.war, which is part of the standard Struts distribution. For a web app
using Spring’s MVC framework, you can use the webapp-minimal application that ships with
Spring. Both of these are nice starting points, but neither simplifies the Struts-Spring integration
nor takes into account unit testing. Therefore, I created Equinox, both for this book and
to help developers get started quickly with Spring.
Equinox is a bare-bones starter application for creating a Spring-based web application. It has
a pre-defined directory structure, an Ant build file (for compiling, deploying and testing), and
all the JARs you will need for a Struts, Spring and Hibernate-based web app. Much of the
directory structure and build file in Equinox is taken from my open source AppFuse application.
Therefore, Equinox is really just an “AppFuse Light” that allows rapid web app development
with minimal setup. Because it is derived from AppFuse, you will see many references to
it in package names, database names and other areas. This is done purposefully so you can
migrate from an Equinox-based application to a more robust AppFuse-based application.
In order to start MyUsers, download Equinox 1.0 from sourcebeat.com/downloads and
extract it to an appropriate location.
Download equinox: http://www.sourcebeat.com/downloads
Create project directories and an Ant
build file
• To set up your initial directory structure and Ant build file, extract the
Equinox downloadonto your hard drive. I recommend putting
projects in C:Source on Windows and ~/devon Unix or Linux. For
Windows users, now is a good time set your HOME environment
variable to C:Source. The easiest way to get started with Equinox
is to extract it to your preferred source location, cd into the equinox
directory and run ant new -Dapp.name=myusers from the
command line.
• Warning: You must have the CATALINA_HOME environment
variable set, or building MyUsers will not work. This is because its
build.xml depends on a catalina-ant.jar file for running Tomcat’s Ant
tasks (covered shortly). As an alternative, you can specify a
Tomcat.home property in build.properties that points to your Tomcat
installation.
Directory structure
Type ant in the MyUsers directory
to see options
[echo] Available targets are:
[echo] compile --> Compile all Java files
[echo] war --> Package as WAR file
[echo] deploy --> Deploy application as directory
[echo] deploywar --> Deploy application as a WAR file
[echo] install --> Install application in Tomcat
[echo] remove --> Remove application from Tomcat
[echo] reload --> Reload application in Tomcat
[echo] start --> Start Tomcat application
[echo] stop --> Stop Tomcat application
[echo] list --> List Tomcat applications
[echo] clean --> Deletes compiled classes and WAR
[echo] new --> Creates a new project
Tomcat and Ant
Tomcat ships with a number of Ant tasks that allow you to
install, remove and reload web apps using its Manager
application. The easiest way to declare and use these
tasks is to create a properties file that contains all the
definitions. In Equinox, a tomcatTasks.properties file is in
the base directory with contents:
deploy=org.apache.catalina.ant.DeployTask
undeploy=org.apache.catalina.ant.UndeployTask
remove=org.apache.catalina.ant.RemoveTask
reload=org.apache.catalina.ant.ReloadTask
start=org.apache.catalina.ant.StartTask
stop=org.apache.catalina.ant.StopTask
list=org.apache.catalina.ant.ListTask
More of build.xml
<!-- Tomcat Ant Tasks -->
<taskdef file="tomcatTasks.properties">
<classpath>
<pathelement
path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<target name="install" description="Install application in Tomcat"
depends="war">
<deploy url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"
war="file:${dist.dir}/${webapp.name}.war"/>
</target>
<target name="remove" description="Remove application from Tomcat">
<undeploy url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"/>
</target>
<target name="reload" description="Reload application in Tomcat">
Build continued
<reload url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"/>
</target>
<target name="start" description="Start Tomcat application">
<start url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"/>
</target>
<target name="stop" description="Stop Tomcat application">
<stop url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"/>
</target>
<target name="list" description="List Tomcat applications">
<list url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"/>
</target>
Build.properties
# Properties for Tomcat Server
tomcat.manager.url=http://localhost:8080/mana
ger
tomcat.manager.username=admin
tomcat.manager.password=admin
• Edit $CATALINA_HOME/conf/tomcat-
users.xml to make sure this line is there.
<user username="admin"
password="admin" roles="manager"/>
Blackscreen test…type ant listC:equinox-1.0myusers>ant list
Buildfile: build.xml
list:
[list] OK - Listed applications for virtual host localhost
[list] /:running:0:ROOT
[list] /manager:running:0:manager
[list] /StatefulCounterEJB:running:0:StatefulCounterEJB
[list] /myusers:running:0:myusers
[list] /ejb-examples-1.1:running:0:ejb-examples-1.1
[list] /blazeds:running:0:blazeds
[list] /HelloBean:running:0:HelloBean
[list] /jndiservlet:running:0:jndiservlet
[list] /docs:running:0:docs
[list] /openejb:running:0:openejb
[list] /LookupServlet:running:0:LookupServlet
[list] /ejb-examples:running:0:ejb-examples
[list] /myexamples:running:0:myexamples
[list] /hello-openejb:running:0:hello-openejb
[list] /HibernateWithMySQL:running:0:HibernateWithMySQL
[list] /examples:running:0:examples
[list] /host-manager:running:0:host-manager
[list] /HelloEjbService:running:0:
[list] /HibernateApp3:running:0:HibernateApp3
[list] /CurrentQuoteEJB:running:0:CurrentQuoteEJB
[list] /FtoCEJB:running:0:FtoCEJB
BUILD SUCCESSFUL
Total time: 1 second
C:equinox-1.0myusers>
installation
• Running ant deploy will deploy myusers.
Test by going to 8080/myusers
Running the HSQLDB
• Warning: In order for the in-memory
HSQLDB to work correctly with MyUsers,
start Tomcat from the same directory from
which you run Ant. Type
$CATALINA_HOME/bin/startup.sh on
Unix/Linux and %CATALINA_HOME
%binstartup.bat on Windows. You can
also change the database settings to use
an absolute path.
Spring hibernate tutorial
Create a UserDAOTest.java
1. Create a UserDAOTest.java class in the test/org/appfuse/dao directory. This class should
extend BaseDAOTestCase, which already exists in this package. This parent class
initializes Spring’s ApplicationContext from the web/WEB-INF/applicationContext.xml file.
Listing 2.6 is the code you will need to begin writing your UserDAOTest.java:
Listing 2.6
Note: Automatically importing necessary classes is a nice feature of modern IDEs like
Eclipse and IDEA. Equinox contains the project necessary for both of these IDEs.
This class won’t compile yet because you haven’t created your UserDAO interface. Before you
do that, write a couple of tests to verify CRUD works on the User object.
2. Add the testSave and testAddAndRemove methods to the UserDAOTest class, as
shown in Listing 2.7:
Listing 2.7
package org.appfuse.dao;
// use your IDE to handle imports
public class UserDAOTest extends BaseDAOTestCase {
private User user = null;
private UserDAO dao = null;
protected void setUp() throws Exception {
super.setUp();
dao = (UserDAO) ctx.getBean("userDAO");
}
protected void tearDown() throws Exception {
super.tearDown();
dao = null;
}
}
You’ll need to use netbeans or eclipse add imports
(or search for them yourself)
• Add the testSave and testAddAndRemove methods to the UserDAOTest class, as
• shown
package org.appfuse.dao;
// use your IDE to handle imports
public class UserDAOTest extends BaseDAOTestCase {
private User user = null;
private UserDAO dao = null;
protected void setUp() throws Exception {
super.setUp();
dao = (UserDAO) ctx.getBean("userDAO");
}
protected void tearDown() throws Exception {
super.tearDown();
dao = null;}}
public void testSaveUser() throws Exception {
user = new User();
user.setFirstName("Rod");
user.setLastName("Johnson");dao.saveUser(user);
assertNotNull("primary key assigned", user.getId());
log.info(user);
assertNotNull(user.getFirstName());
}
public void testAddAndRemoveUser() throws Exception {
user = new User();
user.setFirstName("Bill");
user.setLastName("Joy");
dao.saveUser(user);
assertNotNull(user.getId());
assertEquals(user.getFirstName(), "Bill");
if (log.isDebugEnabled()) {
log.debug("removing user...");
}
dao.removeUser(user.getId());
assertNull(dao.getUser(user.getId()));
}
UserDAO
• From these test methods, you can see that you need to
create a UserDAO with the following
• methods:
• saveUser(User)
• removeUser(Long)
• getUser(Long)
• getUsers() (to return all the users in the database)
• 3. Create a UserDAO.java file in the src/org/appfuse/dao
directory and populate it with the
• code in Listing 2.8:
Create a UserDAO.java file in the
src/org/appfuse/dao directory
• package org.appfuse.dao;
• // use your IDE to handle imports
• public interface UserDAO extends DAO {
• public List getUsers();
• public User getUser(Long userId);
• public void saveUser(User user);
• public void removeUser(Long userId);
• }
• Finally, in order for the UserDAOTest and
UserDAO to compile, create a User object
to persist.
• Create a User.java class in the
src/org/appfuse/model directory and add
id, firstName
• and lastName as member variables, as
shown
class User
• package org.appfuse.model;
• public class User extends BaseObject {
• private Long id;
• private String firstName;
• private String lastName;
• /*
• Generate your getters and setters using your favorite
IDE:
• In Eclipse:
• Right-click -> Source -> Generate Getters and Setters
• */
• }
• Notice that you’re extending a
BaseObject class which is already
defined. It has methods: toString(),
equals() and hashCode(). The latter two
are required by Hibernate.
create a file named User.hbm.xml
• In the src/org/appfuse/model directory, create a file named User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="org.appfuse.model.User" table="app_user">
<id name="id" column="id" unsaved-value="0">
<generator class="increment" />
</id>
<property name="firstName" column="first_name"
not-null="true"/>
<property name="lastName" column="last_name" not-null="true"/>
</class>
</hibernate-mapping>
Edit applicationContext.xml
• Add this mapping to Spring’s applicationContext.xml file in the
web/WEB-INF directory. Open this file and look for <property
name="mappingResources"> and change it to :
<property name="mappingResources">
<list>
<value>org/appfuse/model/User.hbm.xml</value>
</list>
</property>
• In the applicationContext.xml file, you can see how the database is
set up and Hibernate is configured to work with Spring. Equinox is
designed to work with an HSQL database named db/appfuse. It will
be created in your project’s db directory. Details of this configuration
will be covered in the “How Spring is configured in Equinox” section.
Run ant deploy reload to rebuild
and redeploy
• INFO - SchemaExport.execute(98) | Running hbm2ddl schema
export
• INFO - SchemaExport.execute(117) | exporting generated schema
to database
• INFO - ConnectionProviderFactory.newConnectionProvider(53) |
Initializing
• connection provider: org.springframework.orm.hibernate
• .LocalDataSourceConnectionProvider
• INFO -
DriverManagerDataSource.getConnectionFromDriverManager(140)
|
• Creating new JDBC connection to [jdbc:hsqldb:db/appfuse]
• INFO - SchemaExport.execute(160) | schema export complete
run ant browse to
bring up a HSQL console to see the table
(Tomcat must be running)
How Spring is configured in Equinox
• It is very easy to configure any J2EE-based web application to use
Spring. At the very least, you can simply add Spring’s
ContextLoaderListener to your web.xml file
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
• This is a ServletContextListener that initializes when your web app
starts up. By default, it looks for Spring’s configuration file at WEB-
INF/applicationContext.xml. You can change this default value by
specifying a <context-param> element named contextConfig-
Location.
how does Spring know about
Hibernate?
• Look at the full contents of your applicationContext.xml file
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/sampleContext.xml</param-value>
</context-param>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:db/appfuse</value>
</property>
<property name="username"><value>sa</value></property>
<!-- Make sure <value> tags are on same line - if they're not,
authentication will fail -->
<property name="password"><value></value></property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>org/appfuse/model/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
continued
<prop key="hibernate.dialect">
net.sf.hibernate.dialect.HSQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
• The first bean (dataSource) represents an HSQL database, and the
second bean (session-Factory) has a dependency on that bean.
Spring just calls setDataSource (DataSource) on the
LocalSessionFactoryBean to make this work. If you wanted to use a
Java Naming and Directory Interface (JNDI) DataSource instead,
you could easily change this bean’s definition to something similar
to
• <bean id="dataSource"
• class="org.springframework.jndi.JndiObjectFactoryBean">
• <property name="jndiName">
• <value>java:comp/env/jdbc/appfuse</value>
• </property>
• </bean>
• Also note the hibernate.hbm2ddl.auto property in the
sessionFactory definition. This property creates the
database tables automatically when the application
starts. Other possible values are update and create-
drop.The last bean configured is the
transactionManager (and nothing is stopping you from
using a Java Transaction API — JTA — transaction
manager), which is necessary to perform distributed
transactions across two databases. If you want to use a
JTA transaction manager, simply change this bean’s
class attribute to org.springframework.transaction.jta.Jta-
TransactionManager. Now you can implement the
UserDAO with Hibernate.
The UserDAOHibernate.java class in
src/org/appfuse/dao/hibernate (create dir
first)
package org.appfuse.dao.hibernate;
// organize imports using your IDE
public class UserDAOHibernate extends HibernateDaoSupport implements
UserDAO {
private Log log = LogFactory.getLog(UserDAOHibernate.class);
public List getUsers() {
return getHibernateTemplate().find("from User");
}
public User getUser(Long id) {
return (User) getHibernateTemplate().get(User.class, id);
}
public void saveUser(User user) {
getHibernateTemplate().saveOrUpdate(user);
if (log.isDebugEnabled()) {
log.debug("userId set to: " + user.getId());
}
}
public void removeUser(Long id) {
Object user = getHibernateTemplate().load(User.class, id);
getHibernateTemplate().delete(user);
}
}
• Spring’s HibernateDaoSupport class is a
convenient super class for Hibernate DAOs. It
has
• handy methods you can call to get a Hibernate
Session, or a SessionFactory. The most
convenient
• method is getHibernateTemplate(), which
returns a HibernateTemplate. This
• template wraps Hibernate checked exceptions
with runtime exceptions, allowing your DAO
• interfaces to be Hibernate exception-free.
• Nothing is in your application to bind UserDAO to
UserDAOHibernate, so you must create
• that relationship.
• 2. For Spring to recognize the relationship, add the lines
in Listing 2.18 to the web/WEBINF/
• applicationContext.xml file.
<bean id="userDAO"
class="org.appfuse.dao.hibernate.UserDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
Note url…edit/add user
Added users…

More Related Content

PDF
Leverage Hibernate and Spring Features Together
Edureka!
 
DOCX
Spring notes
Rajeev Uppala
 
PDF
Hibernate complete notes_by_sekhar_sir_javabynatara_j
Satya Johnny
 
PDF
Hibernate Interview Questions
Syed Shahul
 
PPTX
Building web applications with Java & Spring
David Kiss
 
PDF
Spring core module
Raj Tomar
 
PPTX
Spring framework
Rajkumar Singh
 
PPTX
Spring MVC framework
Mohit Gupta
 
Leverage Hibernate and Spring Features Together
Edureka!
 
Spring notes
Rajeev Uppala
 
Hibernate complete notes_by_sekhar_sir_javabynatara_j
Satya Johnny
 
Hibernate Interview Questions
Syed Shahul
 
Building web applications with Java & Spring
David Kiss
 
Spring core module
Raj Tomar
 
Spring framework
Rajkumar Singh
 
Spring MVC framework
Mohit Gupta
 

What's hot (20)

DOCX
Struts notes
Rajeev Uppala
 
PDF
Java spring framework
Rajiv Gupta
 
PPTX
Spring Framework Rohit
Rohit Prabhakar
 
PDF
Spring framework Introduction
Anuj Singh Rajput
 
PPTX
Introduction to Spring Framework
Raveendra R
 
PPTX
Spring Framework
tola99
 
PDF
Spring framework
vietduc17
 
ODP
Spring User Guide
Muthuselvam RS
 
PPTX
Java Spring Framework
Mehul Jariwala
 
PPT
Java & J2EE Struts with Hibernate Framework
Mohit Belwal
 
PDF
Spring bean mod02
Guo Albert
 
PPTX
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Arjun Thakur
 
PDF
Spring Framework - Core
Dzmitry Naskou
 
PPTX
Multithreading in java
JanmejayaPadhiary2
 
ODP
Introduction to Spring Framework and Spring IoC
Funnelll
 
PPTX
Project Presentation on Advance Java
Vikas Goyal
 
PDF
Spring MVC Framework
Hùng Nguyễn Huy
 
PPTX
Spring framework in depth
Vinay Kumar
 
PPTX
Java spring ppt
natashasweety7
 
Struts notes
Rajeev Uppala
 
Java spring framework
Rajiv Gupta
 
Spring Framework Rohit
Rohit Prabhakar
 
Spring framework Introduction
Anuj Singh Rajput
 
Introduction to Spring Framework
Raveendra R
 
Spring Framework
tola99
 
Spring framework
vietduc17
 
Spring User Guide
Muthuselvam RS
 
Java Spring Framework
Mehul Jariwala
 
Java & J2EE Struts with Hibernate Framework
Mohit Belwal
 
Spring bean mod02
Guo Albert
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Arjun Thakur
 
Spring Framework - Core
Dzmitry Naskou
 
Multithreading in java
JanmejayaPadhiary2
 
Introduction to Spring Framework and Spring IoC
Funnelll
 
Project Presentation on Advance Java
Vikas Goyal
 
Spring MVC Framework
Hùng Nguyễn Huy
 
Spring framework in depth
Vinay Kumar
 
Java spring ppt
natashasweety7
 
Ad

Viewers also liked (11)

PDF
Spring and hibernate santosh'.pdf
Satya Johnny
 
DOC
Html refrence notes
Gunjan Verma
 
PDF
Webservises natraj -satya
Satya Johnny
 
PDF
Spring hibernate natraj
Satya Johnny
 
PDF
Hibernate complete notes_by_sekhar_sir_javabynatara_j
Satya Johnny
 
PDF
Hibernate natraj - satya
Satya Johnny
 
PPTX
Hibernate working with criteria- Basic Introduction
Er. Gaurav Kumar
 
PPTX
Spring & hibernate
Santosh Kumar Kar
 
DOC
Html, xml and java script
Rajeev Uppala
 
ODP
Hibernate Developer Reference
Muthuselvam RS
 
PDF
Hibernate Presentation
guest11106b
 
Spring and hibernate santosh'.pdf
Satya Johnny
 
Html refrence notes
Gunjan Verma
 
Webservises natraj -satya
Satya Johnny
 
Spring hibernate natraj
Satya Johnny
 
Hibernate complete notes_by_sekhar_sir_javabynatara_j
Satya Johnny
 
Hibernate natraj - satya
Satya Johnny
 
Hibernate working with criteria- Basic Introduction
Er. Gaurav Kumar
 
Spring & hibernate
Santosh Kumar Kar
 
Html, xml and java script
Rajeev Uppala
 
Hibernate Developer Reference
Muthuselvam RS
 
Hibernate Presentation
guest11106b
 
Ad

Similar to Spring hibernate tutorial (20)

PDF
Spring Live Sample Chapter
Syed Shahul
 
PPT
Maven Introduction
Sandeep Chawla
 
PDF
What's New in AppFuse 2.0
Matt Raible
 
PPTX
Flask
Mamta Kumari
 
PDF
Microservices in Java
Anatole Tresch
 
PDF
Seven Simple Reasons to Use AppFuse
Matt Raible
 
PPTX
Dockerization of Azure Platform
nirajrules
 
PPT
Maven: Managing Software Projects for Repeatable Results
Steve Keener
 
PPTX
Lecture 1 Introduction to React Native.pptx
GevitaChinnaiah
 
PPT
What's new in p2 (2009)?
Pascal Rapicault
 
DOCX
BLCN532 Lab 1Set up your development environmentV2.0.docx
moirarandell
 
PDF
Chapter6 web apps-tomcat
Venkat Gowda
 
PPTX
Django simplified : by weever mbakaya
Mbakaya Kwatukha
 
PPTX
1 app 2 developers 3 servers
Mark Myers
 
PPTX
How create react app help in creating a new react applications
Concetto Labs
 
PDF
Nike pop up habitat
Chef
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PPTX
Quick Interview Preparation Dot Net Core
Karmanjay Verma
 
PDF
Lecture11_LaravelGetStarted_SPring2023.pdf
ShaimaaMohamedGalal
 
Spring Live Sample Chapter
Syed Shahul
 
Maven Introduction
Sandeep Chawla
 
What's New in AppFuse 2.0
Matt Raible
 
Microservices in Java
Anatole Tresch
 
Seven Simple Reasons to Use AppFuse
Matt Raible
 
Dockerization of Azure Platform
nirajrules
 
Maven: Managing Software Projects for Repeatable Results
Steve Keener
 
Lecture 1 Introduction to React Native.pptx
GevitaChinnaiah
 
What's new in p2 (2009)?
Pascal Rapicault
 
BLCN532 Lab 1Set up your development environmentV2.0.docx
moirarandell
 
Chapter6 web apps-tomcat
Venkat Gowda
 
Django simplified : by weever mbakaya
Mbakaya Kwatukha
 
1 app 2 developers 3 servers
Mark Myers
 
How create react app help in creating a new react applications
Concetto Labs
 
Nike pop up habitat
Chef
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
Quick Interview Preparation Dot Net Core
Karmanjay Verma
 
Lecture11_LaravelGetStarted_SPring2023.pdf
ShaimaaMohamedGalal
 

Recently uploaded (20)

PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Understanding operators in c language.pptx
auteharshil95
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 

Spring hibernate tutorial

  • 1. Spring-Hibernate tutorial Spring Live (chapter 2 posted online)
  • 2. What is appfuse? • AppFuse is an open-source Java EE web application framework. It is designed for quick and easy start up of development, while also using open- source Java technologies such as Spring framework, Hibernate and Struts. AppFuse was originally created by Matt Raible, who wanted to eliminate the "ramp up" time in building new web applications. • AppFuse provides a project skeleton, similar to the one that's created by an IDE when one clicks through a "new web project" wizard. AppFuse 1.x uses Ant to create the project, as well as build/test/deploy it, whereas AppFuse 2.x uses Maven 2 for these tasks. IDE support was improved in 2.0 by leveraging Maven plugins to generate IDE project files. AppFuse 1.x uses XDoclet and JDK 1.4+. • Unlike other "new project" wizards, the AppFuse wizard creates a number of additional classes and files that implement features, but also serve as examples for the developer. The project is pre-configured to talk to a database, to deploy in an appserver, and to allow logging in. • When AppFuse was first developed, it only supported Struts and Hibernate. In version 2.x, it supports Hibernate, iBATIS or JPA as persistence frameworks. For implementing the MVC model, AppFuse is compatible with JSF, Spring MVC, Struts 2 or Tapestry.
  • 3. Appfuse • Features integrated into AppFuse includes the following: • Authentication and Authorization • Remember Me (saving your login information so you don't have to login every time) • /Registration • SSL Switching • E-Mail • URL rewriting • Skinnability • Page Decoration • Templated Layout • File Upload • This out-of-the-box functionality is one of the main features in AppFuse that separates it from the other "CRUD Generation" frameworks, including Ruby on Rails and Grails. The aforementioned framework, as well as AppFuse, allow you to create master/detail pages from database tables or existing model objects.
  • 4. Tutorials posted with code • This tutorial is posted at • http://www.scribd.com/doc/7074123/Spring-Live-Matt-Raible • A newer tutorial has been posted at • http://www.sourcebeat.com/titles/springlive/public/Rev_12/Spr • code online for both tutorials: • http:// svn.sourcebeat.com/svn/sourcebeat/trunk/downloads/
  • 6. The tutorial covers Below are the ordered steps you will perform: 1. Download Struts and Spring. 2. Create project directories and an Ant build file. 3. Create a unit test for the persistence layer. 4. Configure Hibernate and Spring. 5. Create Hibernate DAO implementation. 6. Run the unit test and verify CRUD with DAO. 7. Create manager and declare transactions. 8. Create a unit test for the Struts action. 9. Create an action and model (DynaActionForm) for the web layer. 10. Run the unit test and verify CRUD with action. 11. Create JavaServer Pages (JSPs) to allow CRUD through a web browser. 12. Verify the JSPs’ functionality through your browser. 13. Add Validation use Commons Validator.
  • 7. Downloads and environment variables Download Struts and Spring1 1. Download and install the following components: 􀀗 Java Development Kit (JDK) 1.4.2 (or above) 􀀗 Tomcat 5.0+ 􀀗 Ant 1.6.1+ 2. Set up the following environment variables: 􀀗 JAVA_HOME 􀀗 ANT_HOME 􀀗 CATALINA_HOME 3. Add the following to your PATH environment variable: 􀀗 JAVA_HOME/bin 􀀗 ANT_HOME/bin 􀀗 CATALINA_HOME/bin
  • 8. The approach To develop a Java-based web application, developers usually download JARs, create a directory structure, and create an Ant build file. For a Struts-only application, this can be simplified by using the struts-blank.war, which is part of the standard Struts distribution. For a web app using Spring’s MVC framework, you can use the webapp-minimal application that ships with Spring. Both of these are nice starting points, but neither simplifies the Struts-Spring integration nor takes into account unit testing. Therefore, I created Equinox, both for this book and to help developers get started quickly with Spring. Equinox is a bare-bones starter application for creating a Spring-based web application. It has a pre-defined directory structure, an Ant build file (for compiling, deploying and testing), and all the JARs you will need for a Struts, Spring and Hibernate-based web app. Much of the directory structure and build file in Equinox is taken from my open source AppFuse application. Therefore, Equinox is really just an “AppFuse Light” that allows rapid web app development with minimal setup. Because it is derived from AppFuse, you will see many references to it in package names, database names and other areas. This is done purposefully so you can migrate from an Equinox-based application to a more robust AppFuse-based application. In order to start MyUsers, download Equinox 1.0 from sourcebeat.com/downloads and extract it to an appropriate location. Download equinox: http://www.sourcebeat.com/downloads
  • 9. Create project directories and an Ant build file • To set up your initial directory structure and Ant build file, extract the Equinox downloadonto your hard drive. I recommend putting projects in C:Source on Windows and ~/devon Unix or Linux. For Windows users, now is a good time set your HOME environment variable to C:Source. The easiest way to get started with Equinox is to extract it to your preferred source location, cd into the equinox directory and run ant new -Dapp.name=myusers from the command line. • Warning: You must have the CATALINA_HOME environment variable set, or building MyUsers will not work. This is because its build.xml depends on a catalina-ant.jar file for running Tomcat’s Ant tasks (covered shortly). As an alternative, you can specify a Tomcat.home property in build.properties that points to your Tomcat installation.
  • 11. Type ant in the MyUsers directory to see options [echo] Available targets are: [echo] compile --> Compile all Java files [echo] war --> Package as WAR file [echo] deploy --> Deploy application as directory [echo] deploywar --> Deploy application as a WAR file [echo] install --> Install application in Tomcat [echo] remove --> Remove application from Tomcat [echo] reload --> Reload application in Tomcat [echo] start --> Start Tomcat application [echo] stop --> Stop Tomcat application [echo] list --> List Tomcat applications [echo] clean --> Deletes compiled classes and WAR [echo] new --> Creates a new project
  • 12. Tomcat and Ant Tomcat ships with a number of Ant tasks that allow you to install, remove and reload web apps using its Manager application. The easiest way to declare and use these tasks is to create a properties file that contains all the definitions. In Equinox, a tomcatTasks.properties file is in the base directory with contents: deploy=org.apache.catalina.ant.DeployTask undeploy=org.apache.catalina.ant.UndeployTask remove=org.apache.catalina.ant.RemoveTask reload=org.apache.catalina.ant.ReloadTask start=org.apache.catalina.ant.StartTask stop=org.apache.catalina.ant.StopTask list=org.apache.catalina.ant.ListTask
  • 13. More of build.xml <!-- Tomcat Ant Tasks --> <taskdef file="tomcatTasks.properties"> <classpath> <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/> </classpath> </taskdef> <target name="install" description="Install application in Tomcat" depends="war"> <deploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${webapp.name}" war="file:${dist.dir}/${webapp.name}.war"/> </target> <target name="remove" description="Remove application from Tomcat"> <undeploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${webapp.name}"/> </target> <target name="reload" description="Reload application in Tomcat">
  • 14. Build continued <reload url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${webapp.name}"/> </target> <target name="start" description="Start Tomcat application"> <start url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${webapp.name}"/> </target> <target name="stop" description="Stop Tomcat application"> <stop url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${webapp.name}"/> </target> <target name="list" description="List Tomcat applications"> <list url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}"/> </target>
  • 15. Build.properties # Properties for Tomcat Server tomcat.manager.url=http://localhost:8080/mana ger tomcat.manager.username=admin tomcat.manager.password=admin • Edit $CATALINA_HOME/conf/tomcat- users.xml to make sure this line is there. <user username="admin" password="admin" roles="manager"/>
  • 16. Blackscreen test…type ant listC:equinox-1.0myusers>ant list Buildfile: build.xml list: [list] OK - Listed applications for virtual host localhost [list] /:running:0:ROOT [list] /manager:running:0:manager [list] /StatefulCounterEJB:running:0:StatefulCounterEJB [list] /myusers:running:0:myusers [list] /ejb-examples-1.1:running:0:ejb-examples-1.1 [list] /blazeds:running:0:blazeds [list] /HelloBean:running:0:HelloBean [list] /jndiservlet:running:0:jndiservlet [list] /docs:running:0:docs [list] /openejb:running:0:openejb [list] /LookupServlet:running:0:LookupServlet [list] /ejb-examples:running:0:ejb-examples [list] /myexamples:running:0:myexamples [list] /hello-openejb:running:0:hello-openejb [list] /HibernateWithMySQL:running:0:HibernateWithMySQL [list] /examples:running:0:examples [list] /host-manager:running:0:host-manager [list] /HelloEjbService:running:0: [list] /HibernateApp3:running:0:HibernateApp3 [list] /CurrentQuoteEJB:running:0:CurrentQuoteEJB [list] /FtoCEJB:running:0:FtoCEJB BUILD SUCCESSFUL Total time: 1 second C:equinox-1.0myusers>
  • 17. installation • Running ant deploy will deploy myusers.
  • 18. Test by going to 8080/myusers
  • 19. Running the HSQLDB • Warning: In order for the in-memory HSQLDB to work correctly with MyUsers, start Tomcat from the same directory from which you run Ant. Type $CATALINA_HOME/bin/startup.sh on Unix/Linux and %CATALINA_HOME %binstartup.bat on Windows. You can also change the database settings to use an absolute path.
  • 21. Create a UserDAOTest.java 1. Create a UserDAOTest.java class in the test/org/appfuse/dao directory. This class should extend BaseDAOTestCase, which already exists in this package. This parent class initializes Spring’s ApplicationContext from the web/WEB-INF/applicationContext.xml file. Listing 2.6 is the code you will need to begin writing your UserDAOTest.java: Listing 2.6 Note: Automatically importing necessary classes is a nice feature of modern IDEs like Eclipse and IDEA. Equinox contains the project necessary for both of these IDEs. This class won’t compile yet because you haven’t created your UserDAO interface. Before you do that, write a couple of tests to verify CRUD works on the User object. 2. Add the testSave and testAddAndRemove methods to the UserDAOTest class, as shown in Listing 2.7: Listing 2.7 package org.appfuse.dao; // use your IDE to handle imports public class UserDAOTest extends BaseDAOTestCase { private User user = null; private UserDAO dao = null; protected void setUp() throws Exception { super.setUp(); dao = (UserDAO) ctx.getBean("userDAO"); } protected void tearDown() throws Exception { super.tearDown(); dao = null; } }
  • 22. You’ll need to use netbeans or eclipse add imports (or search for them yourself) • Add the testSave and testAddAndRemove methods to the UserDAOTest class, as • shown package org.appfuse.dao; // use your IDE to handle imports public class UserDAOTest extends BaseDAOTestCase { private User user = null; private UserDAO dao = null; protected void setUp() throws Exception { super.setUp(); dao = (UserDAO) ctx.getBean("userDAO"); } protected void tearDown() throws Exception { super.tearDown(); dao = null;}} public void testSaveUser() throws Exception { user = new User(); user.setFirstName("Rod"); user.setLastName("Johnson");dao.saveUser(user); assertNotNull("primary key assigned", user.getId()); log.info(user); assertNotNull(user.getFirstName()); } public void testAddAndRemoveUser() throws Exception { user = new User(); user.setFirstName("Bill"); user.setLastName("Joy"); dao.saveUser(user); assertNotNull(user.getId()); assertEquals(user.getFirstName(), "Bill"); if (log.isDebugEnabled()) { log.debug("removing user..."); } dao.removeUser(user.getId()); assertNull(dao.getUser(user.getId())); }
  • 23. UserDAO • From these test methods, you can see that you need to create a UserDAO with the following • methods: • saveUser(User) • removeUser(Long) • getUser(Long) • getUsers() (to return all the users in the database) • 3. Create a UserDAO.java file in the src/org/appfuse/dao directory and populate it with the • code in Listing 2.8:
  • 24. Create a UserDAO.java file in the src/org/appfuse/dao directory • package org.appfuse.dao; • // use your IDE to handle imports • public interface UserDAO extends DAO { • public List getUsers(); • public User getUser(Long userId); • public void saveUser(User user); • public void removeUser(Long userId); • }
  • 25. • Finally, in order for the UserDAOTest and UserDAO to compile, create a User object to persist. • Create a User.java class in the src/org/appfuse/model directory and add id, firstName • and lastName as member variables, as shown
  • 26. class User • package org.appfuse.model; • public class User extends BaseObject { • private Long id; • private String firstName; • private String lastName; • /* • Generate your getters and setters using your favorite IDE: • In Eclipse: • Right-click -> Source -> Generate Getters and Setters • */ • }
  • 27. • Notice that you’re extending a BaseObject class which is already defined. It has methods: toString(), equals() and hashCode(). The latter two are required by Hibernate.
  • 28. create a file named User.hbm.xml • In the src/org/appfuse/model directory, create a file named User.hbm.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="org.appfuse.model.User" table="app_user"> <id name="id" column="id" unsaved-value="0"> <generator class="increment" /> </id> <property name="firstName" column="first_name" not-null="true"/> <property name="lastName" column="last_name" not-null="true"/> </class> </hibernate-mapping>
  • 29. Edit applicationContext.xml • Add this mapping to Spring’s applicationContext.xml file in the web/WEB-INF directory. Open this file and look for <property name="mappingResources"> and change it to : <property name="mappingResources"> <list> <value>org/appfuse/model/User.hbm.xml</value> </list> </property> • In the applicationContext.xml file, you can see how the database is set up and Hibernate is configured to work with Spring. Equinox is designed to work with an HSQL database named db/appfuse. It will be created in your project’s db directory. Details of this configuration will be covered in the “How Spring is configured in Equinox” section.
  • 30. Run ant deploy reload to rebuild and redeploy • INFO - SchemaExport.execute(98) | Running hbm2ddl schema export • INFO - SchemaExport.execute(117) | exporting generated schema to database • INFO - ConnectionProviderFactory.newConnectionProvider(53) | Initializing • connection provider: org.springframework.orm.hibernate • .LocalDataSourceConnectionProvider • INFO - DriverManagerDataSource.getConnectionFromDriverManager(140) | • Creating new JDBC connection to [jdbc:hsqldb:db/appfuse] • INFO - SchemaExport.execute(160) | schema export complete
  • 31. run ant browse to bring up a HSQL console to see the table (Tomcat must be running)
  • 32. How Spring is configured in Equinox • It is very easy to configure any J2EE-based web application to use Spring. At the very least, you can simply add Spring’s ContextLoaderListener to your web.xml file <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> • This is a ServletContextListener that initializes when your web app starts up. By default, it looks for Spring’s configuration file at WEB- INF/applicationContext.xml. You can change this default value by specifying a <context-param> element named contextConfig- Location.
  • 33. how does Spring know about Hibernate? • Look at the full contents of your applicationContext.xml file <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/sampleContext.xml</param-value> </context-param> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.hsqldb.jdbcDriver</value> </property> <property name="url"> <value>jdbc:hsqldb:db/appfuse</value> </property> <property name="username"><value>sa</value></property> <!-- Make sure <value> tags are on same line - if they're not, authentication will fail --> <property name="password"><value></value></property> </bean> <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingResources"> <list> <value>org/appfuse/model/User.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props>
  • 34. continued <prop key="hibernate.dialect"> net.sf.hibernate.dialect.HSQLDialect </prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean> <!-- Transaction manager for a single Hibernate SessionFactory --> <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> </beans>
  • 35. • The first bean (dataSource) represents an HSQL database, and the second bean (session-Factory) has a dependency on that bean. Spring just calls setDataSource (DataSource) on the LocalSessionFactoryBean to make this work. If you wanted to use a Java Naming and Directory Interface (JNDI) DataSource instead, you could easily change this bean’s definition to something similar to • <bean id="dataSource" • class="org.springframework.jndi.JndiObjectFactoryBean"> • <property name="jndiName"> • <value>java:comp/env/jdbc/appfuse</value> • </property> • </bean>
  • 36. • Also note the hibernate.hbm2ddl.auto property in the sessionFactory definition. This property creates the database tables automatically when the application starts. Other possible values are update and create- drop.The last bean configured is the transactionManager (and nothing is stopping you from using a Java Transaction API — JTA — transaction manager), which is necessary to perform distributed transactions across two databases. If you want to use a JTA transaction manager, simply change this bean’s class attribute to org.springframework.transaction.jta.Jta- TransactionManager. Now you can implement the UserDAO with Hibernate.
  • 37. The UserDAOHibernate.java class in src/org/appfuse/dao/hibernate (create dir first) package org.appfuse.dao.hibernate; // organize imports using your IDE public class UserDAOHibernate extends HibernateDaoSupport implements UserDAO { private Log log = LogFactory.getLog(UserDAOHibernate.class); public List getUsers() { return getHibernateTemplate().find("from User"); } public User getUser(Long id) { return (User) getHibernateTemplate().get(User.class, id); } public void saveUser(User user) { getHibernateTemplate().saveOrUpdate(user); if (log.isDebugEnabled()) { log.debug("userId set to: " + user.getId()); } } public void removeUser(Long id) { Object user = getHibernateTemplate().load(User.class, id); getHibernateTemplate().delete(user); } }
  • 38. • Spring’s HibernateDaoSupport class is a convenient super class for Hibernate DAOs. It has • handy methods you can call to get a Hibernate Session, or a SessionFactory. The most convenient • method is getHibernateTemplate(), which returns a HibernateTemplate. This • template wraps Hibernate checked exceptions with runtime exceptions, allowing your DAO • interfaces to be Hibernate exception-free.
  • 39. • Nothing is in your application to bind UserDAO to UserDAOHibernate, so you must create • that relationship. • 2. For Spring to recognize the relationship, add the lines in Listing 2.18 to the web/WEBINF/ • applicationContext.xml file. <bean id="userDAO" class="org.appfuse.dao.hibernate.UserDAOHibernate"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean>