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.
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.62. Tomcat 6.0.183. Spring Framework 2.54. Hibernate 3.25. MyEclipse 6.06. MySQL 5.0
Step 1. Includes the jar files within your classpath .
hibernate-annotations3.4.0GA.jarhibernate-commons-annotations3.3.0GA.jarhibernate-core-3.3.1.GA.jarhibernate-entitymanager-3.4.0.GA.jarhibernate-validator-3.1.0.GA.jarcommons-dbcp-1.2.2.jarstandard.jarjstl.jarspring-2.5.jarspring-beans-2.5.jarspring-context-2.5.jarspring-core-2.5.jarspring-jdbc-3.0.0.RELEASE.jarspring-web-2.5.jarspring-webmvc-2.5.jarjboss-archive-browing.jarjbossall-client.jarcommons-collections-3.1.jardom4j-1.6.1.jarejb3-persistence.jarslf4j-api-1.5.6.jarslf4j-simple-1.5.6.jarmysql-connector-java3.1.7.jarcommons-pool-1.4.jarjavaassist-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;@Repositorypublic class UserDaoImpl implements UserDao {private EntityManager entityManagerFactory;@PersistenceContextpublic void setEntityManagerFactory(EntityManager entityManagerFactory) { this.entityManagerFactory = entityManagerFactory;}@Transactionalpublic 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


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