Tasklist Hibernate sessionFactory injection

  • For our simple tasklist tool we will use the simple method of accessing hibernate inside Sakai
    We will create a Hibernate SessionFactory from the Sakai SessionFactoryBase using our Task.hbm.xml file
  • The full path to our HBM file is: org/sakaiproject/tool/tasklist/hibernate/Task.hbm.xml
  • The tasklist manager impl is: org.sakaiproject.tool.tasklist.impl.TaskListManagerDaoImpl
  • The tasklist manager bean id should be: org.sakaiproject.tool.tasklist.api.TaskListManagerDao


  1. All changes should be made to the tasklist spring configuration file (tasklist/tool/src/webapp/WEB-INF/spring-beans.xml)
    • Creating a sessionFactory from the Sakai sessionFactoryBase is less desirable than using the global Sakai sessionFactory but it works better for small tools, the following should be added to your spring configuration file
    1. First we need to define a sessionFactory bean from the Sakai SessionFactoryBase and give it our HBM files (you can include as many HBM files are need to here)
      <bean id="org.sakaiproject.yourapp.dao.SessionFactory"
      	parent="org.sakaiproject.springframework.orm.hibernate.SessionFactoryBase">
      	<property name="mappingResources">
      		<list>
      			<value>org/sakaiproject/myapp/impl/hbm/Item1.hbm.xml</value>
      			<value>org/sakaiproject/myapp/impl/hbm/Item2.hbm.xml</value>
      		</list>
      	</property>
      </bean>
      
      • This is creating a seperate sessionFactory from the global Sakai session factory which is based on the Sakai global session factory
      • This will create a SessionFactory bean called org.sakaiproject.yourapp.SessionFactory which we will could inject into our manager or dao. For this example, we will use it to create a Spring HibernateTemplate.
    2. Define a HibernateTransactionManager by injecting the SessionFactory defined above (org.sakaiproject.yourapp.dao.SessionFactory) to help manage transactions
      <bean id="org.sakaiproject.yourapp.dao.HibernateTransactionManager"
      	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      	<property name="sessionFactory">
      		<ref bean="org.sakaiproject.yourapp.dao.SessionFactory" />
      	</property>
      </bean>
      
      • This is always a good idea but it also buys you the ability to mix JDBC access and hibernate without causing problems
    3. Inject the hibernate SessionFactory bean (org.sakaiproject.yourapp.dao.SessionFactory) into our hibernate implementation of our DAO API
      Note: name it as a target since we do not want to access this directly or we will be outside of the transaction
      <bean id="org.sakaiproject.yourapp.dao.MyAppDaoHibernateTarget"
      	class="org.sakaiproject.yourapp.dao.impl.MyAppDaoHibernateImpl">
      	<property name="sessionFactory">
      		<ref bean="org.sakaiproject.yourapp.dao.SessionFactory" />
      	</property>
      </bean>
      
      • This is where we wire in our code to the hibernate sessionFactory, the class used here (MyAppDaoHibernateImpl) must extend HibernateDaoSupport
    4. Define a declarative transaction interceptor (TransactionProxyFactoryBean) using the TransactionManager (org.sakaiproject.yourapp.dao.HibernateTransactionManager) and pointing it at our DAO(org.sakaiproject.yourapp.dao.MyAppDaoHibernateTarget)
       
      <bean id="org.sakaiproject.yourapp.dao.MyAppDao"
      	class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      	<property name="transactionManager">
      		<ref bean="org.sakaiproject.yourapp.dao.HibernateTransactionManager" />
      	</property>
      	<property name="target">
      		<ref bean="org.sakaiproject.yourapp.dao.MyAppDaoHibernateTarget" />
      	</property>
      	<property name="transactionAttributes">
      		<props>
      			<prop key="*">PROPAGATION_REQUIRED</prop>
      		</props>
      	</property>
      </bean>
      
      • This will do a lot of nice things for us, like manage our rollbacks and propogate changes
    5. Spring file samples
  2. Tie in the new Manager to the Service
    • Add the new manager bean and comment out the memory implementation
      <bean id="org_sakaiproject_tool_tasklist_api_TaskListService"
      	class="org.sakaiproject.tool.tasklist.impl.TaskListServiceImpl"
      	singleton="true">
      	<property name="taskListManager">
      		<ref bean="org.sakaiproject.tool.tasklist.api.TaskListManagerDao" />
      <!--		<ref bean="org.sakaiproject.tool.tasklist.api.TaskListManagerMemory" />-->
      	</property>
      	<property name="toolManager">
      		<ref bean="org.sakaiproject.tool.api.ToolManager" />
      	</property>
      	<property name="userDirectoryService">
      		<ref bean="org.sakaiproject.user.api.UserDirectoryService" />
      	</property>
      </bean>
      
  3. The completed spring configuration file for the tasklist tool is available