-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: HibernateException: No Hibernate Session bound to thread
PostPosted: Tue Aug 30, 2011 4:40 pm 
Newbie

Joined: Thu Jul 28, 2011 2:54 pm
Posts: 12
I'm using Spring 3.0 with ibernate and I've the follwing exception when the hibernate method createSQLQuery(...) is excecuted:

Code:
org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet .java:710)
javax.servlet.http.HttpServlet.service(HttpServlet .java:803)

root cause

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.orm.hibernate3.AbstractSession FactoryBean$TransactionAwareInvocationHandler.invo ke(AbstractSessionFactoryBean.java:299)
$Proxy10.getCurrentSession(Unknown Source)
com.stockdomain.domain.stock.StockDAOImpl.getRecor d(StockDAOImpl.java:32)
com.stockdomain.bo.StockBO.getStock1(StockBO.java: 154)
com.stockdomain.bo.StockBO.getStock1(StockBO.java: 83)
com.stockdomain.common.controller.StockController. submitForm(StockController.java:96)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet .java:710)
javax.servlet.http.HttpServlet.service(HttpServlet .java:803)


The dispatcher-servlet.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
   
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
         
   <context:component-scan base-package="com.stockdomain">
   </context:component-scan>
   
         .....

</beans>



The root context


Code:
<?xml version="1.0" encoding="UTF-8"?>
         
<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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-3.0.xsd">         
         

    <!-- dataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName"><value>org.postgresql.Driver</value></property>
      <property name="url"><value>jdbc:postgresql://localhost/db_finance</value></property>
      <property name="username"><value>xxxx</value></property>
      <property name="password"><value>xxxx</value></property>
    </bean> 
 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
      </props>
      </property>
   </bean>
   
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
               <ref bean="sessionFactory" />
        </property>
    </bean>
   
    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>            
   
</beans>



The java code

Code:
package com.stockdomain.domain.stock;

import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.stockdomain.util.DbUtilities;

@Repository
@Transactional
public class StockDAOImpl implements StockDAO {

   @Autowired
   SessionFactory sessionFactory;

   @SuppressWarnings("unchecked")
   @Transactional
   public List<Stock> getRecord(HashMap<String, Object> whereClauseElements, List<String> orderByElements) {
      String whereClause = DbUtilities.constructWhereClause(whereClauseElements, true);
      String orderByClause = DbUtilities.constructOrderClause(orderByElements);
      String sql = "SELECT * FROM stock " + whereClause + orderByClause;
      Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);
      return query.list();
   }
}



Many thanks in advance !


Top
 Profile  
 
 Post subject: Re: HibernateException: No Hibernate Session bound to thread
PostPosted: Mon Sep 12, 2011 8:25 am 
Newbie

Joined: Thu Jul 28, 2011 2:54 pm
Posts: 12
I made some progress, but have another exception :

Quote:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.MappingException: Unknown entity: com.stockdomain.domain.Stock
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)




Meanwhile, I'll post the changes I did.
I added "<property name="annotatedClasses">" into the session bean Id.


the context xml

Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
           <list>
                <value>com.stockdomain.domain.Stock</value>
           </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
         </props>
      </property>
   </bean>



I also removed @Transactional annotation on the method in StockDAOImpl.java.
the class looks like this now.

Code:
package com.stockdomain.domain.stock;

import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.stockdomain.util.DbUtilities;

@Repository("stockDao")
@Transactional
public class StockDAOImpl implements StockDAO {

    @Autowired
    SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    @Transactional
    public List<Stock> getRecord() {
        String sql = "SELECT * FROM stock ";
      Query query = sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity(Stock.class);
        return query.list();
    }
}


I'll post a new thread for this new exception.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.