Using the Sakai global sessionFactory
- This is the preferred way to use Hibernate in Sakai, however, it requires strict usage of the Sakai tool structure (app,api,impl,hbm,pack), the following should be added to your components.xml file (it must be named components.xml)
- Note: you must deploy your hbm files and value object implementations to shared and you must deploy your components and api-implementations to components or the global session factory will not be able to work with your HBM files
- Define additional hibernate mappings
This allows us to add our HBMs directly to the global Sakai SessionFactory bean
Note: You should place your HBM files and your value object implementations in the same location, this will make them easier to deploy to shared since Hibernate needs to access both of them<bean id="org.sakaiproject.yourapp.hibernate.AdditionalHibernateMappings" class="org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappingsImpl"> <property name="mappingResources"> <list> <value>org/sakaiproject/yourapp/impl/hbm/Item1.hbm.xml</value> <value>org/sakaiproject/yourapp/impl/hbm/Item2.hbm.xml</value> </list> </property> </bean>
- Define a manager implementation bean based on your manager implementation (org.sakaiproject.tool.yourapp.impl.MyToolManagerDaoImpl) which uses the Sakai GlobalSessionFactory
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.MyToolDaoTarget" class="org.sakaiproject.yourapp.dao.impl.MyToolDaoImpl"> <property name="sessionFactory"> <ref bean="org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory" /> </property> </bean>
- Define a declarative transaction interceptor (TransactionProxyFactoryBean) using the GlobalTransactionManager(org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager) and pointing it at our tool DAO (org.sakaiproject.yourapp.dao.MyToolDaoTarget)
<bean id="org.sakaiproject.yourapp.dao.MyToolDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager" /> </property> <property name="target"> <ref bean="org.sakaiproject.yourapp.dao.MyToolDaoTarget"/> </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
- Spring config samples (components.xml)
- A sample file is attached to this page: components.xml
- A sample tool which uses Hibernate in this way: tasklist-complex