Java – No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

hibernatejava

What is this error about? "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here".
My spring config file looks something like this.

<bean id="jndiDataSource"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>java:/devDS</value>
    </property>
</bean>
<bean id="stsaDBFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="jndiDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>xx.yy.zz.User</value>
            <value>xx.yy.UserResponse</value>

        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbmddl.auto">create</prop>
        </props>
    </property>
</bean>

<!-- ################################### Aspects ################################################## -->

<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref local="stsaDBFactory" />
    </property>
</bean>

All the DAO test passes when i test them outside of the container using junit. When I deploy it in jBoss as a portal app,I get this exception. Also it works fine if i remove the portal specific configuration and make it a simple web app and deploy it on jboss.Any idea?

Best Answer

You have defined a TransactionManager in your spring config but you are trying to execute a hibernate query in a method that is not transactional. Try adding @Transactional to your method or class.

Related Topic