Integrating Spring with Hibernate:
I have user Hibernate 5 and Spring 3 libraries for below implementation.
Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="org.dinesh" />
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="HR" />
<property name="password" value="HR" />
</bean>
<bean id="hibernatDaoImpl" class="org.dinesh.dao.SimpleHibernateDaoImpl" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds" />
</bean>
<!-- org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
org.springframework.orm.hibernate4.LocalSessionFactoryBean
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="ds" />
<property name="packagesToScan" value="org.dinesh" />
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
package org.dinesh.dao.HibernateDaoImpl;
import org.dinesh.dao.SimpleHibernateDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HibernateImpl
{
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
SimpleHibernateDaoImpl dao = (SimpleHibernateDaoImpl) ctx.getBean("hibernatDaoImpl");
System.out.println(dao.getCircleCount());
}
}
package org.dinesh.dao;
import javax.transaction.Transaction;
import org.dinesh.model.Student;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class SimpleHibernateDaoImpl {
//have to initialize the session factory and have to inject in this dao
@Autowired
private SessionFactory sessionFactory;
public int getCircleCount()
{
String hql="select count(*) from Student";
Session session=getSessionFactory().openSession();
org.hibernate.Transaction t =session.beginTransaction();
Query query=session.createQuery(hql);
Student s= new Student(10, "Dinesh");
session.persist(s);
t.commit();
return ((Long)query.uniqueResult()).intValue();
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
No comments:
Post a Comment