In this post we will see an example of how to integrate hibernate with spring. The steps
are self explanatory as they are very similar to the steps for using JdbcTemplate.
Points to ponder:
- @Entity : This annotation states that the POJO class as an entity.
- @Id : This annotation declares the identifier property of this entity.
- <context:annotation-config/> : It is used to activate annotations in beans already registered in the application context.
- <context:component-scan/> : It scans packages to find and register beans within the application context.
- @Repository : This annotation is a stereotype for persistence layer. It indicates that the class defines a data repository.
- @Autowired : @Autowired indicates that spring should inject dependency, based on its type or based on its name if used with the @Qualifier annotation.
File: com.ram.model.Departments.java
package com.ram.model; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Departments { @Id private int department_id; private String department_name; private int manager_id; private int location_id; public int getDepartment_id() { return department_id; } public void setDepartment_id(int department_id) { this.department_id = department_id; } public String getDepartment_name() { return department_name; } public void setDepartment_name(String department_name) { this.department_name = department_name; } public int getManager_id() { return manager_id; } public void setManager_id(int manager_id) { this.manager_id = manager_id; } public int getLocation_id() { return location_id; } public void setLocation_id(int location_id) { this.location_id = location_id; } }
Next create applicationContext.xml file under src. The file is given below.
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.ram.dao"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <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"/> <property name="initialSize" value="2"/> <property name="maxActive" value="5"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.ram.model"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> </beans>
File: com.ram.dao.DepartmentsDaoImpl.java
package com.ram.dao; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class DepartmentsDaoImpl { @Autowired private SessionFactory sessionFactory; public int getDepartmentsCount(){ String hql = "select count(*) from Departments"; Query query = getSessionFactory().openSession().createQuery(hql); return ((Long)query.uniqueResult()).intValue(); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
File: com.ram.main.DepartmentsApp.java
package com.ram.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ram.dao.DepartmentsDaoImpl; public class DepartmentsApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); DepartmentsDaoImpl departmentsDaoImpl = context.getBean("departmentsDaoImpl",DepartmentsDaoImpl.class); System.out.println("Total rows in departments table = "+departmentsDaoImpl.getDepartmentsCount()); } }
No comments:
Post a Comment