Child pages
  • Creating sessions from the Sakai SessionFactoryBase
Skip to end of metadata
Go to start of metadata
  • 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
  • No labels