Announcement Announcement Module
No announcement yet.
Moving from Hibernate to JPA but need legacy access to sessionFactory - transactions? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Moving from Hibernate to JPA but need legacy access to sessionFactory - transactions?

    Hey guys,

    I am moving a project (partly) from the Hibernate interface to the JPA interface, but for legacy reasons I still need access to the underlying sessionFactory. So I set up the following applicationContext.xml:

    	<bean id="dataSource"
    		<property name="driverClassName" value="xx" />
    		<property name="url" value="xx" />
    		<property name="username" value="xx" />
    		<property name="password" value="xx" />
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        	<property name="dataSource" ref="dataSource"/>
        	<property name="persistenceXmlLocation" value="META-INF/persistence.xml"/>
        	<property name="jpaVendorAdapter">
            	<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                	<property name="showSql" value="false"/>
    		<property name="packagesToScan">
    		<property name="jpaDialect">
                 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    		<property name="jpaProperties">
    		<prop key="hibernate.current_session_context_class">thread</prop>
    		<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    		<prop key="">update</prop>
    		<prop key="hibernate.show_sql">false</prop>
        <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        <!-- legacy access to session factory -->
    	<bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory" />
        <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    <tx:annotation-driven transaction-manager="txManager" />
    Now the problem is that transactions don't work when accessed from the sessionFactory. They work fine when using EntityManager directly. When I replace txManager by the one in comments, it doesn't work for sessionFactory and it doesn't work for EntityManager either.

    I have no idea how I can fix this issue. I have tried everything but I simply cannot get transactions to work.

    Below is an example function I would use sessionFactory in:

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void doSomething() {
    Session session = sessionFactory.getCurrentSession();
    Bla bla = new Bla();; // throws an error that there is no active transaction