Monday, August 25, 2014

How to integrate Spring and Hibernate

Here are the few tips I have gathered on how to integrate spring and hibernate.

Create your bean.xml something like this...

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<bean id="usrMgmtServiceImpl"   class="com.usermanagement.service.impl.UserManagementServiceImpl">
<property name="privService" ref="privService"/>
<property name="roleService" ref="roleService"/>
<property name="userService" ref="userService"/>
</bean>

<!--               DB CONFIGURATION BEGINS  -->

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>Connection.properties</value></property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
<property name="driverClassName">  
<value>${db.driverClassName}</value>
</property>  
<property name="url">  
<value>${db.url}</value>
</property>
<property name="username">  
<value>${db.username}</value>
</property>
<property name="password">  
<value>${db.password}</value>
</property>
</bean>

<bean id="mysessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- These are the POJO objects, you have to mention these here... -->
<property name="annotatedClasses">
<list>
<value>com.usermanagement.pojo.User</value>
<value>com.usermanagement.pojo.Role</value>
<value>com.usermanagement.pojo.Privilege</value>
<value>com.usermanagement.pojo.Password</value>
<value>com.usermanagement.pojo.PasswordPolicy</value>
</list>
</property>  

<property name="hibernateProperties">  
<props>  
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.cache.use_second_level_cache">true</prop> 
<!--<prop key="hibernate.cache.provider_class">org.hibernate.cache.SingletonEhCacheProvider</prop>-->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
</props>  
</property>  
</bean>  
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">  
<property name="sessionFactory" ref="mysessionFactory"></property>  
</bean>      
<!--               DB CONFIGURATION ENDS  --> 


<bean id="privService" class="com.usermanagement.service.impl.PrivilegeServiceImpl">
<property name="privDao" ref="privDao"/>
</bean>
<bean id="roleService" class="com.usermanagement.service.impl.RoleServiceImpl">
<property name="roleDao" ref="roleDao"/>
</bean>
<bean id="userService" class="com.usermanagement.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>



<bean id="privDao" class="com.usermanagement.dao.impl.PrivilegeDAOImpl" >
<property name="template" ref="template"></property>
</bean>
<bean id="roleDao" class="com.usermanagement.dao.impl.RoleDAOImpl" >
<property name="template" ref="template"></property>
</bean>
<bean id="userDao" class="com.usermanagement.dao.impl.UserDAOImpl">
<property name="template" ref="template"></property>
</bean>

</beans>

The list of jars that you might need are
  1. antlr-2.7.6.jar
  2. asm-1.5.3.jar
  3. axis2-spring-1.6.1.jar
  4. cglib-2.1_3.jar
  5. commons-collections-3.1.jar
  6. commons-dbcp-1.4.jar
  7. commons-logging-1.0.4.jar
  8. commons-pool-1.6.jar
  9. dom4j-1.6.1.jar
  10. hibernate-3.2.0.ga.jar
  11. hibernate-3.2.6.jar
  12. hibernate-annotations-3.4.0.GA.jar
  13. hibernate-commons-annotations-3.1.0.GA.jar
  14. hibernate-core-3.3.0.SP1.jar
  15. hibernate-entitymanager.jar
  16. hibernate-jpa-2.0-api-1.0.0.Final.jar
  17. jta-1.1.jar
  18. log4j-1.2.14.jar
  19. ojdbc14.jar
  20. org.springframework.asm-3.0.0.RELEASE.jar
  21. org.springframework.beans-3.0.0.RELEASE.jar
  22. org.springframework.context-3.0.0.RELEASE.jar
  23. org.springframework.core-3.0.0.RELEASE.jar
  24. org.springframework.expression-3.0.0.RELEASE.jar
  25. org.springframework.jdbc-3.0.0.RELEASE.jar
  26. org.springframework.orm-3.0.0.RELEASE.jar
  27. org.springframework.web.servlet-3.0.1.RELEASE-A.jar
  28. slf4j-api-1.6.1.jar
  29. spring-tx-3.0.0.RELEASE.jar


No comments:

Post a Comment