Announcement Announcement Module
Collapse
No announcement yet.
Moving from Hibernate to JPA but need legacy access to sessionFactory - transactions? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="xx" />
    		<property name="url" value="xx" />
    		<property name="username" value="xx" />
    		<property name="password" value="xx" />
    	</bean>
    	
    	<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"/>
             	</bean>
         	</property>
    		<property name="packagesToScan">
    			<list>
    				<value>xx</value>
    			</list>
    		</property>
    		<property name="jpaDialect">
                 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
            </property>
    		<property name="jpaProperties">
    		<props>
    		<prop key="hibernate.current_session_context_class">thread</prop>
    		<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    		<prop key="hibernate.hbm2ddl.auto">update</prop>
    		<prop key="hibernate.show_sql">false</prop>
    		</props>
    		</property>
       </bean>
       
    	
    	
        <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    	
        
        <!-- 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" />
    	</bean>
    -->
    <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:

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