Upload
santhosh-chiluka
View
243
Download
0
Embed Size (px)
Citation preview
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 1/31
Create Database Connection
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 2/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 3/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 4/31
Create Java Project
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 5/31
Add Hibernate Capabilities
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 6/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 7/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 8/31
Select springandhibernate connection from the list. Other data is populated automatically.
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 9/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 10/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 11/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 12/31
Add Spring Capabilities
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 13/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 14/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 15/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 16/31
Hibernate Reverse Engineering
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 17/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 18/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 19/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 20/31
Make two new java files in com.dps.springhibernate
Create two files BusinessLogic.java and PersistenceLayer.java.
Source Code of BusinessLogic.java
package com.dps.springhibernate;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class BusinessLogic {
public static void main(String[] args) {
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 21/31
/* 1. Create a new user */
Integer id = new Integer(3);
User user = new User();user.setId(id);
user.setUsername("jdoe");
user.setPassword("1234");user.setFirstname("John");
user.setLastname("Doe");
user.setDatecreated("2009");
/* 2. Load the Spring bean configuration and create a bean factory */
BeanFactory beanFactory = new XmlBeanFactory(new
ClassPathResource("applicationContext.xml"));
/* 3. Create instance of PersistenceLayer */
PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory.getBean("persistenceLayer");
/* 4. Save the new user to the database */
persistenceLayer.addUser(user);
/* 5. Confirm that our user was saved */User userLoadedFromDB = persistenceLayer.findUserById(id);
System.out.println("User Loaded from DB [username="
+ userLoadedFromDB.getUsername() + ", password="+ userLoadedFromDB.getPassword() + ", firstName="
+ userLoadedFromDB.getFirstname() + ", lastName="
+ userLoadedFromDB.getLastname() + "]");
/* 6. Update the user */
userLoadedFromDB.setFirstname("Johnathan");persistenceLayer.updateUser(userLoadedFromDB);
/* 7. Confirm that the update worked */
User userLoadedFromDBAgain = persistenceLayer .findUserById(id);
System.out.println("User Loaded from DB Again [firstName="
+ userLoadedFromDBAgain.getFirstname() + "]");
/* 8. Delete the user */
persistenceLayer.deleteUser(user);}
}
Source Code of PersistenceLayer.java
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 22/31
package com.dps.springhibernate;
public class PersistenceLayer {
private UserDAO userDAO;
publicUserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void addUser(User user) {
userDAO.save(user);
}
public User findUserById(Integer id) {
return userDAO.findById(id);
}
public void updateUser(User user) {
userDAO.merge(user);
}
public void deleteUser(User user) {
userDAO.delete(user);
}
}
Configuration bean:
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 23/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 24/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 25/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 26/31
On properties tab click on Add Class Properties.
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 27/31
Select userDAO and click on edit.
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 28/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 29/31
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 30/31
Running as BusinessLogin.java as Java Application
8/7/2019 Tutorial of Integration of spring with hibernate
http://slidepdf.com/reader/full/tutorial-of-integration-of-spring-with-hibernate 31/31
Output:
User Loaded from DB [username=jdoe, password=1234, firstName=John,
lastName=Doe]User Loaded from DB Again [firstName=Johnathan]
Resource Website:
http://www.myeclipseide.com/documentation/quickstarts/hibernateandspring/