views:

280

answers:

3

Hi there,

I'm trying to get my spring DAOs to work but I only get this exception

PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'sessionFactory' threw exception; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

my applicationContext.xml looks like this

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;

 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

    <bean id="ds1Datasource" class="org.springframework.jndi.JndiObjectFactoryBean">
     <property name="jndiName" value="java:comp/env/jdbc/ds1"/>
  </bean>

 <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="db2Datasource"/>
    <property name="mappingResources">
      <list>
        <value>hibernate/P1.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.DB2400Dialect
        hibernate.connection.autocommit=false
        hibernate.connection.charset=UTF-8
        hibernate.show_sql=true
      </value>
    </property>
  </bean>

<tx:annotation-driven transaction-manager="myTxManager"/> 
<bean id="myTxManager"  class="org.springframework.transaction.jta.JtaTransactionManager" />


  <bean id="p1" class="dao.DomainP1Impl">
   <property name="sessionFactory" ref="mySessionFactory" />
  </bean>

And my DAO like this

@Transactional
public class DomainP1Impl implements DomainP1 {...}

What I'm doing wrong? Forgotten to mention: I'm using a JBoss AS 4.2.3

+1  A: 

Add this to the session factory bean (mySessionFactory):

<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>

And then see explanation in http://www.theresearchkitchen.com/blog/archives/73.

Bruno Rothgiesser
This works but one thing: If I use the Spring managed transactions, I receive this exception too.
asrijaal
Think I've got it: hibernate.current_session_context_class=thread wasn't set in my config, thats why it doesn't worked with spring managed transactions.
asrijaal
SessionFactory is created, my bean too. But no query gets executed, am I missing something else?
asrijaal
A: 

Stupid question maybe, but how are you getting the session in your dao?

You should be using "getCurrentSession()" to get the session, or better still extend spring's HibernateDAOSupport for your dto's.

Your config is the same as one we used on a recent project and had none of the issues you're having. Did not have to do any more configuration than you've shown in your original question. We did however always extend HibernateDAOSupport in our DAO's. And we were also on Jboss.

I'm not sure you should be setting that property to false (exposeTransactionAwareSessionFactory) - it should work without it.

I've found the javadocs for LocalSessionFactoryBean to be particularly helpful.

Michael Wiles
I'm using the "getCurrentSession()" method, I'll extend my DAO with the HibernateDAOSupport and test it.I've encountered another problem/exception: Proxy$xx cannot be cast against DomainP1Impl - well it is programmed againt an interface so I don't really know whats going wrong here.
asrijaal
A: 

No query gets executed because your datasources are all screwed up ds1Datasource and db2Datasource

<bean id="ds1Datasource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/ds1"/>
         </bean>

 <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="db2Datasource"/>
non sequitor