Spring with Hibernate

Welcome To The Tripathi Spring with Hibernate Page

Step by step configuration of Spring , Hibernate and JPA

I am sharing a demo application which have been created in spring ,hibernate and JPA .This application give helps you to understand the basic configuration of spring,
hibernate and jpa.
Before to understand the example , first we know the brief knowledge about JPA.
What is JPA?
Java Persistence API for persisting the Java Objects to relational database using ORM technology.The JPA API can be used to persist the business object (POJO) to the relational database. Retrieving the data from database in the form of business objects (Java Objects) is so simple with the help of JPA API.
JPA is not a product and can’t be used as it is for persistence. It needs an ORM implementation to work and persist the Java Objects. ORM frameworks that can be used with JPA are Hibernate, Toplink, Open JPA etc.
Start the example :
This example provide step by step instructions to understand the configuration.
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

Step 1. Includes the jar files within your classpath .
hibernate-annotations3.4.0GA.jar
hibernate-commons-annotations3.3.0GA.jar
hibernate-core-3.3.1.GA.jar
hibernate-entitymanager-3.4.0.GA.jar
hibernate-validator-3.1.0.GA.jar
commons-dbcp-1.2.2.jar
standard.jar
jstl.jar
spring-2.5.jar
spring-beans-2.5.jar
spring-context-2.5.jar
spring-core-2.5.jar
spring-jdbc-3.0.0.RELEASE.jar
spring-web-2.5.jar
spring-webmvc-2.5.jar
jboss-archive-browing.jar
jbossall-client.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
slf4j-api-1.5.6.jar
slf4j-simple-1.5.6.jar
mysql-connector-java3.1.7.jar
commons-pool-1.4.jar
javaassist-client.jar

Step 2 : Create a user table in database schema within your database.
CREATE TABLE  `databaseName`.`user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(45) NOT NULL,
`lastname` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 3: Looks around the project packaging structure .






Step 4: With in this step ,i am creating the user pojo ,controller and dao class.
1 create a User.java class within x.y.z.bean package.
package x.y.z.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User {
private Integer id;
private String firstName;
private String lastName;
private String address;
public User() {
 
}


2. Now create a UserService Interface within x.y.z.service package
package x.y.z.service;
 
import x.y.z.bean.User;
 
public interface UserService {
public boolean addUser(User user);
}

3. Creates a UserServiceImpl class within x.y.z.serviceImpl package
package x.y.z.serviceImpl;
import x.y.z.bean.User;
import x.y.z.dao.UserDao;
import x.y.z.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public boolean addUser(User user) {
 userDao.userDao(user);
 return true;
}
public void setUserDao(UserDao userDao) {
 this.userDao = userDao;
}
}

4. Creates a UserDao Interface within x.y.z.dao package
package x.y.z.dao;
import x.y.z.bean.User;
public interface UserDao {
public boolean userDao(User user);
}

5. Creates a UserDaoImpl Class within x.y.z.daoImpl package
package x.y.z.daoImpl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import x.y.z.bean.User;
import x.y.z.dao.UserDao;
@Repository
public class UserDaoImpl implements UserDao {
private EntityManager entityManagerFactory;
@PersistenceContext
public void setEntityManagerFactory(EntityManager entityManagerFactory) {
 this.entityManagerFactory = entityManagerFactory;
}
 
@Transactional
public boolean userDao(User user) {
 entityManagerFactory.merge(user);
 return true;
}
}

6. Creates a UserController Class within x.y.z.controller package
package x.y.z.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import x.y.z.bean.User;
import x.y.z.service.UserService;
public class UserController extends SimpleFormController {
UserService userService;
 
public UserController() {
 setCommandClass(User.class);
 setCommandName("save");
}
 
public ModelAndView onSubmit(Object obj) {
 User user = (User) obj;
 userService.addUser(user);
 return new ModelAndView("success");
}
 
public void setUserService(UserService userService) {
 this.userService = userService;
}
}

Step 5 : Create a configuration file web.xml,applicationContext.xml ,persistent.xml and web-servlet.xml.
1. persistent.xml file
2. web-servlet.xml file 

3. applicationContext.xml file
4. web.xml file
Step 6 : Create a jsp file (success.jsp,index.jsp,registerMe.jsp) within jsp folder
1. success.jsp
Print a success message.
2. index.jsp
2. registerMe.jsp

1 comment:

  1. this blog is very informative .one of the recommanded blog best regardssbrtrainings
    mongo DB online training

    ReplyDelete