Hi all!

I am trying to migrate an application to Spring. The problem is that I don't want to (or can't) migrate the whole app to Spring at the same time, so I am trying to migrate only parts of the app. I successfully configured DI for one part and now I am trying to get the descriptive transaction approach working - since it's only an annotation and one line in the config file (I wanna use @Transactional).

So I was looking for the correct combination of defining the sessionFactory in Spring and using this sessionFactory with my hibernate setup. I tried the following setups:

#1
Code:
<bean id="sessionFactory"
	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">
				${Common.JDBCMappingDialect}
			</prop>				
                        <prop key="hibernate.show_sql">
				${Common.JDBCVerbose}
			</prop>
			<prop key="hibernate.use_sql_comments">
				${Common.JDBCVerbose}
			</prop>                                
			</props>
	</property>
	<property name="mappingResources">
               [mappings] 
        </property>
</bean>

<bean id="transactionManager"
	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>
#2
Code:
<bean id="transactionManager"
	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
	<property name="hibernateManagedSession" value="true" />
	</bean>

<bean id="sessionFactory"
	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">
				${Common.JDBCMappingDialect}
			</prop>
			<prop key="hibernate.show_sql">
				${Common.JDBCVerbose}
			</prop>
			<prop key="hibernate.use_sql_comments">
				${Common.JDBCVerbose}
			</prop>     
                        <prop key="hibernate.current_session_context_class">
        		        thread
			</prop>                           
			</props>
	</property>
	<property name="mappingResources">
        [mappings] 
        </property>
</bean>
#3
Code:
<bean id="transactionManager"
	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
	<property name="hibernateManagedSession" value="true" />
</bean>

<bean id="sessionFactory"
	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="hibernateProperties">
               <props>
			<prop key="hibernate.dialect">
				${Common.JDBCMappingDialect}
			</prop>
			<prop key="hibernate.show_sql">
				${Common.JDBCVerbose}
			</prop>
			<prop key="hibernate.use_sql_comments">
				${Common.JDBCVerbose}
			</prop>     
                        <prop key="hibernate.current_session_context_class">
			org.springframework.orm.hibernate3.SpringSessionContext
			</prop>                           
			</props>
	</property>
	<property name="mappingResources">
        [mappings] 
        </property>
</bean>
None of the above was working with Spring and Hibernate. One worked with Spring, the other with hibernate, due to different sessions being returned and / or wrong transactional contexts when using the standard
Code:
getCurrentSession().beginTransaction();
etc. in the code or @Transactional annotated methods.

I found a way to get it working, but I don't think that's the intended way (example showing the method starting the transaction for methods that are not annotated):
Code:
public static void beginCurrentTransaction() throws HibernateException
{
	HibernateTransactionManager hibernateTransMgr = 
        (HibernateTransactionManager) appContext.getBean("transactionManager");
	txStatus.set(hibernateTransMgr.getTransaction(null));
}
This setup (#1 and the code used to start a transaction) works for both, hibernate and Spring. So far so good. Is it a nice way to do it? I don't think so, since it took me a couple of hours/days to figure it out. My question now is: Is there like a best practice or a "Hey that's the way to go" method to operate both Hibernate transactions and Spring managed transactions using annotations?

Every comment / hint is much appreciated as I am not really happy with this solution although it seems to be working. I'm hoping to find a nicer way to do this! Thanks in advance.

Cheers.