Wednesday 29 April 2020

Spring and Hibernate Integration Application


Spring 4 .0  and Hibernate 4.0  Integration Application 


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.

We are going to integrate the hibernate application with spring, we don't need to create the hibernate.cfg.xml file.
We can provide all the information in the applicationContext.xml file.

Steps
Let's see what are the simple steps for hibernate and spring integration:

  • First Create a Java Application 
  • create applicationContext.xml file It contains information of DataSource, SessionFactory etc.
  • create User.java file It is the persistent class
  • create UserDao.java file It is the dao class that uses HibernateTemplate.
  • create SpringHibernateApplication.java  with main method .


For Spring Hibernate application Configuration download jar files by click here 


applicationContext.xml 

In this file, we are providing all the informations of the database in the BasicDataSource object.
This object is used in the LocalSessionFactoryBean class object, containing some other informations 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.

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
          http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
          http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
          http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
">

<context:annotation-config />
<context:component-scan base-package="in.jk" />
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="postgresDatasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url"
value="jdbc:postgresql://localhost:5432/postgres"></property>
<property name="driverClassName"
value="org.postgresql.Driver"></property>
<property name="username" value="postgres"></property>
<property name="password" value="jk123"></property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="postgresDatasource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
  <property name="annotatedClasses">
<list>
<value>in.jk.User</value>
</list>
</property>
</bean>
<bean id="template"
class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="hibernateTemplate" class="in.jk.UserDao">
<property name="hibernateTemplate" ref="template"></property>
</bean>
</beans>

User.java

package in.jk;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Users")
public class User {

@Id
@Column(name = "userId")
private int userId;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;

// Getters and Setters

@Override
public String toString() {
return "User [userId=" + userId + ", name=" + name + ", email=" + email + "]";
}
}


UserDao.java

package in.jk;

import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class UserDao {

HibernateTemplate hibernateTemplate;

public void save(User user) {

hibernateTemplate.save(user);
}

public User get(int id) {

return hibernateTemplate.get(User.class, id);
}

public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}

}


SpringHibernateApplication.java

package in.jk;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringHibernateApplication {

public static void main(String[] args) {

ApplicationContext applicationContext = null;
               applicationContext  = ClassPathXmlApplicationContext("ApplicationContext.xml");
UserDao dao = (UserDao) applicationContext.getBean("hibernateTemplate");

User user = new User();
user.setUserId(100);
user.setName("JK");
user.setEmail("jk@gmail.com");

dao.save(user);
System.out.println("User Added ....");
System.out.println();
        User userobj = dao.get(100);
System.out.println("User From Daatabase ...."+userobj);

}

}

Output in Console  ....










No comments:

Post a Comment