Spring Data- Spring Hibernate

Spring -Hibernate Example

The Spring framework provides HibernateTemplate class, so you don’t need to follow so many steps like create Configuration, BuildSessionFactory, Session, beginning and committing transaction etc.

Commonly used methods of HibernateTemplate class.

Method Description
void persist(Object entity) persists the given object.
Serializable save(Object entity) persists the given object and returns id.
void saveOrUpdate(Object entity) persists or updates the given object. If id is found, it updates the record otherwise saves the record.
void update(Object entity) updates the given object.
void delete(Object entity) deletes the given object on the basis of id.
Object get(Class entityClass, Serializable id) returns the persistent object on the basis of given id.
Object load(Class entityClass, Serializable id) returns the persistent object on the basis of given id.
List loadAll(Class entityClass) returns the all the persistent objects.

Student.java:
It is a simple POJO class. Here it works as the persistent class for hibernate.

pacage smlcodes;
public class Student {
	private int sno;
	private String name;
	private String address;
//Setters & getters
}

Student.hbm.xml:
This mapping file contains all the information of the persistent class.

<hibernate-mapping>
	<class name="smlcodes.Student" table="student">
 <id name="sno">
 	<generator class="assigned"></generator>
 </id>

 <property name="name"></property>
 <property name="address"></property>
	</class>
</hibernate-mapping>

StudentDao.java:
it uses the HibernateTemplate class method to persist the object of Student class.

public class StudentDao {
	HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
 this.template = template;
	}

	public void saveEmployee(Student e) {
 template.save(e);
	}

	public void updateEmployee(Student e) {
 template.update(e);
	}

	public void deleteEmployee(Student e) {
 template.delete(e);
	}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 <property name="url" value="jdbc:mysql://localhost:3306/springdb" />
 <property name="username" value="root" />
 <property name="password" value="root" />
	</bean>

	<bean id="mysessionFactory"
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <property name="dataSource" ref="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">update</prop>
  <prop key="hibernate.show_sql">true</prop>
 	</props>
 </property>
	</bean>

	<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
 <property name="sessionFactory" ref="mysessionFactory"></property>
	</bean>

	<bean id="d" class="smlcodes.StudentDao">
 <property name="template" ref="template"></property>
	</bean>
</beans>

In this file, we are providing all the information’s of the database in the BasicDataSource object. This object is used in the LocalSessionFactoryBean class object, containing some other information’s such as mappingResources and hibernateProperties. The object of LocalSessionFactoryBean class is used in the HibernateTemplate class. Let’s see the code of applicationContext.xml file.

StudentHibernateExample.java

package smlcodes;
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 StudentHibernateExample {
	public static void main(String[] args) {

 Resource r = new ClassPathResource("applicationContext.xml");
 BeanFactory factory = new XmlBeanFactory(r);

 StudentDao dao = (StudentDao) factory.getBean("d");

 Student e = new Student();
 e.setSno(147);
 e.setName("kumar");
 e.setAddress("Hyderabad");

 dao.saveEmployee(e);
 // dao.updateEmployee(e);
	}
}

Remebember: We need to add Hibernate jars as well in this application.