I am going to share the demo example to configure ORM tools like hibernate with spring framework. I have developed this demo application in MyEclipse and used mysql as backend to do a CRUD operation.
Now I am going to explain an example on how I have used spring and Hibernate in a web based application.
Example Environment used:
1. JDK 1.6
2. Tomcat 6.0.18
3. Spring Framework 2.5
4. Hibernate 3.2
5. MyEclipse 6.0
6. MySQL 5.0
Steps include:
1. Setting up Eclipse workspace.
2. Creating a Java Web project with
appropriate name.
3. Creating a source folder and
appropriate packages.
4. Creating a separate folder for
JSP files.
5. Creating WEB-INF, WEB-INF\lib and
WEB-INF\classes folders.
6. Creating WEB-INF\config folder
for keeping Hibernate and Spring specific
configuration files, respectively.
7. Adding appropriate JAR files.
8. Creating a classes in appropriate
packing folder
9. Mapping Hibernate and Spring
10. Deploy and Run the application
I
am starting from the point seven .we need the following jar files.
01.antlr-2.7.6
02.commons-collections-3.1
03.commons-dbcp-1.2.2
04.commons-logging-1.0.4
05.commons-pool-1.4
06.dom4j-1.6.1
07.ejb3-persistence
08.hibernate3
09.hibernate-annotations
10.hibernate-commons-annotations
11.mysql-connector-java3.1.7
12.javassist-3.4.GA
13.jstl
14.jta-1.1
15.slf4j-api-1.5.6
16.slf4j-simple-1.5.6
17.standard
18.spring-2.5.jar
19.spring-beans-2.5.jar
20.spring-context-2.5.jar
21.spring-core-2.5.jar
22.spring-jdbc-3.0.0.RELEASE.jar
23.spring-web-2.5.jar
24.spring-webmvc-2.5.jar
Now
I will do an entry in web.xml file to invoke the spring’s dispatcher servlet.
If
you are not aware about ContextLoaderListener so get more details from below
link
Now
I will create a bean class within my bean package
public class UserRegistrationBean {
privateInteger id;
privateString firstName;
privateString lastName;
privateString email;
privateString mobile;
//Its getter and setter methods
}
Next
I will create service interface and its implementation class within their
respective packages
packagex.y.z.service;
public interface UserService {
public boolean saveUser(UserRegistrationBean
userRegistrationBean);
}
//Its Implementation
packagex.y.z.serviceImpl;
public class UserServiceImpl
implements UserService {
public boolean saveUser(UserRegistrationBean
bean) {
private UserDao userDao;
public boolean saveUser(UserRegistrationBean
userRegistrationBean){
return userDao.saveUserDao(bean);
}
public void setUserDao(UserDao
userDao) {
this.userDao = userDao;
}
}
Next
I will create serviceDao interface and its implementation class within their
respective packages.(this is for database service)
packagex.y.z.serviceDao;
public interface UserDao {
public boolean saveUserDao(UserRegistrationBean
userRegistrationBean);
}
//its
implementation
packagex.y.z.serviceDaoImpl;
public class UserDaoImpl
implements UserDao {
private SessionFactory
sessionFactory;
private Session session;
public boolean saveUserDao(UserRegistrationBean
userRegistrationBean){
session =
getSessionFactory().openSession();
try{
Transaction tx = session.beginTransaction();
session.save(userRegistrationBean);
tx.commit();
} catch(Exception e) {
e.printStackTrace();
returnfalse;
} finally{
session.close();
}
returntrue;
}
public SessionFactory
getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory
sessionFactory) {
this.sessionFactory =
sessionFactory;
}
}
Now
I will create a controller class
public class AddUserController
extends SimpleFormController {
private UserService userService;
public AddUserController() {
setCommandName("registerMe");
setCommandClass(UserRegistrationBean.class);
}
public ModelAndView onSubmit(Object
object) {
UserRegistrationBean
userRegistrationBean=
(UserRegistrationBean)object;
userService.saveUser(userRegistrationBean);
return new ModelAndView("success");
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService
userService) {
this.userService = userService;
}
}
My welcome file is index.jsp:
<taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core>
<tagliburi="http://www.springframework.org/tags/form"prefix="form">
<c:redirecturl="login.htm"/>
Create
a login.jsp under jsp folder.
<form:formcommandName="registerMe"method="post">
//create a table ,inputtext
field and submit button.
//note:input textfield name
should be same as bean name
</form>
Now we will do configuration between spring and hibernate . First we create the hbm configuration file (it is used to map java pojo object to relational database table field
user.hbm.xml file.
This xml file is used for spring bean configuration.
web-servlet.xml
file
This
xml file is used to configure enterprise centric configuration. In our case we
will configure the datasource and hibernate session factory.
applicationContext.xml