Announcement Announcement Module
Collapse
No announcement yet.
Hibernate 4 - No Current Session Error Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate 4 - No Current Session Error

    Good morning,

    I got the "infamous" current session error when I upgraded from Hibernate 3 to 4.
    I search for it and everything seems to be set up properly. I need some insight

    Here is my conf :
    Spring 3.2
    Hibernate 4.1.9

    Datasource 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"
    	xsi:schemaLocation="http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.2.xsd
    	http://www.springframework.org/schema/tx 
    	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    	http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    	
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver">
    		</property>
    		<property name="url"
    			value="jdbc:mysql://xxxxx:3306/db?zeroDateTimeBehavior=convertToNull&amp;autoReconnect=true">
    		</property>
    		<property name="username" value="xxxx"></property>
    		<property name="password" value="xxxx"></property>
    	</bean>
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.MySQLDialect
    				</prop>
    			</props>
    		</property>
    		<property name="mappingResources">
    			<list>
    				<value>
    					com/dao/user/User.hbm.xml
    				</value>						
    			</list>
    		</property>
    	</bean>
    	
    	<context:component-scan base-package="com.dao" />
    	<context:annotation-config />
    	<tx:annotation-driven transaction-manager="hibernateTransactionManager" proxy-target-class="true"/>
    
    	<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="userDAO" class="com.dao.user.UserDAOImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    	
    </beans>
    Business 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:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.2.xsd
    	http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
    	<context:component-scan base-package="com.services" />
    	<context:annotation-config /> 	
    
    	<bean id="userServices" class="com.services.user.UserService">
    		<property name="UserDAO" ref="userDAO" />
    	</bean>		
    </beans>

    My Service Layer:

    Code:
    package com.services.user;
    
    @Service
    @Transactional
    public class UserService implements IUserService{
    
    	@Autowired
    	private UserDAO			userDAO;
    
    	@Override
    	public User findUser(String email) {
    		return this.getEagsUserDAO().findById(email);
    	}
    	
    	public UserDAO getUserDAO() {
    		return this.userDAO;
    	}
    
    	public void setUserDAO(UserDAO userDAO) {
    		this.userDAO = userDAO;
    	}
    }
    Finally, the DAO implementation:

    Code:
    package com.dao.user;
    
    @Repository("userDAO")
    @Transactional
    public class UserDAOImpl implements UserDAO {
    
    	private SessionFactory	sessionFactory;
    
    	@Autowired
    	@Resource(name = "sessionFactory")
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	}
    
    	@Override
    	public User findById(String id) {
    		try {
    			User instance = (User) this.sessionFactory.getCurrentSession().get("com.dao.user.User",id);
    			return instance;
    		} catch (RuntimeException re) {
    			throw re;
    		}
    	}
    }
    And here the stackTrace :

    Code:
    ERROR - UserDAOImpl.findById(111) | get failed
    org.hibernate.HibernateException: No Session found for current thread
    	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
    	at com.dao.user.UserDAOImpl.findById(UserDAOImpl.java:107)
    	at com.dao.user.UserDAOImpl$$FastClassByCGLIB$$bec6ba.invoke(<generated>)
    	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
    	at com.dao.user.UserDAOImpl$$EnhancerByCGLIB$$9b54939b.findById(<generated>)
    	at com.services.user.UserService.findUser(UserService.java:68)
    	at com.services.user.UserService$$FastClassByCGLIB$$41c85d92.invoke(<generated>)
    	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
    	at com.services.user.UserService$$EnhancerByCGLIB$$1bf5e773.findUser(<generated>)
    	at com.beans.login.Login.doLogin(Login.java:100)
    	at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:186)
    	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:166)
    	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    	at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:186)
    	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    Thanks for the help !
Working...
X