Searching...
Friday, 9 August 2013

Spring Hibernate Integration Simple Example

06:17

Here is one example to integrate Spring and Hibernate. Create a java project and add below files and configuration files

Project Structure

image 

1. Create a POJO class (Student.java)

package com.pretech;
public class Student {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

2. Create a DAO class

package com.pretech;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class StudentDao {
	HibernateTemplate template;
	public void setSessionFactory(SessionFactory factory) {
		template = new HibernateTemplate(factory);
	}
	public void saveStudent(Student e) {
		template.save(e);
	}
}

3. Create Spring configuration file (applicationContext.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"  value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/hibernateschema"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
		
	</bean>
	
	<bean id="mysessionFactory"	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource"><ref bean="dataSource" /></property>
		
		<property name="mappingResources">
		<list>
		<value>student.hbm.xml</value>
		</list>
		</property>
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.show_sql">true</prop>
				
			</props>
		</property>
	</bean>
	
	<bean id="d" class="com.pretech.StudentDao">
	<property name="sessionFactory" ref="mysessionFactory"></property>
	</bean>
	
	</beans>

4. Create Hibernate Mapping file (student.hbm.xml)

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.pretech.Student" table="Student" >
        <id name="id" >
            <generator class="assigned" >
            </generator>
        </id>
        <property name="name" >
        </property>
    </class>
</hibernate-mapping>

5. Create a Main class to insert Student details

package com.pretech;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class StudentMain {
	public static void main(String[] args) {
		Resource resource = new ClassPathResource("applicationContext.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		StudentDao dao = (StudentDao) factory.getBean("d");
		Student student = new Student();
		student.setId(101);
		student.setName("Vinod");
		dao.saveStudent(student);
	}
}

6. Output


image 


Download this example Spring Hibernate Example

0 comments: