You are on page 1of 31

Create Database Connection

Create Java Project .

Add Hibernate Capabilities .

.

.

Select springandhibernate connection from the list. . Other data is populated automatically.

.

.

.

Add Spring Capabilities .

.

.

.

Hibernate Reverse Engineering .

.

.

.

dps. import org.springframework.java.factory. import org. Source Code of BusinessLogic.BeanFactory.Make two new java files in com. public class BusinessLogic { public static void main(String[] args) { . import org.dps.springframework.io.java package com.core.XmlBeanFactory.factory.xml.springhibernate.beans.java and PersistenceLayer.ClassPathResource.beans.springframework.springhibernate Create two files BusinessLogic.

/* 1. user. user. Create instance of PersistenceLayer */ PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory .getFirstname() + "]").setDatecreated("2009"). } } Source Code of PersistenceLayer. persistenceLayer. Update the user */ userLoadedFromDB. /* 6. Delete the user */ persistenceLayer.setPassword("1234").addUser(user). /* 4.setLastname("Doe"). user. /* 3.updateUser(userLoadedFromDB). User user = new User().xml")).getPassword() + ". Create a new user */ Integer id = new Integer(3). firstName=" + userLoadedFromDB. System.setUsername("jdoe").java . Load the Spring bean configuration and create a bean factory */ BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource( "applicationContext. /* 8.deleteUser(user).println("User Loaded from DB Again [firstName=" + userLoadedFromDBAgain. /* 5. user. password=" + userLoadedFromDB.setFirstname("John"). Confirm that the update worked */ User userLoadedFromDBAgain = persistenceLayer .println("User Loaded from DB [username=" + userLoadedFromDB.findUserById(id).out.out. Save the new user to the database */ persistenceLayer.findUserById(id). user.getBean("persistenceLayer").getLastname() + "]").getFirstname() + ". /* 2.getUsername() + ". System.setId(id).setFirstname("Johnathan"). Confirm that our user was saved */ User userLoadedFromDB = persistenceLayer. /* 7. user. lastName=" + userLoadedFromDB.

public class PersistenceLayer { private UserDAO userDAO. } public void addUser(User user) { userDAO. } public void updateUser(User user) { userDAO. public UserDAO getUserDAO() { return userDAO.package com.dps.save(user). } } Configuration bean: .merge(user).findById(id).springhibernate. } public void setUserDAO(UserDAO userDAO) { this.delete(user). } public void deleteUser(User user) { userDAO. } public User findUserById(Integer id) { return userDAO.userDAO = userDAO.

.

.

.

.On properties tab click on Add Class Properties.

Select userDAO and click on edit. .

.

.

Running as BusinessLogin.java as Java Application .

lastName=Doe] User Loaded from DB Again [firstName=Johnathan] Resource Website: http://www. firstName=John. password=1234.com/documentation/quickstarts/hibernateandspring/ .Output: User Loaded from DB [username=jdoe.myeclipseide.