Hibernate 4:org.hibernate.HibernateException:找不到当前线程的Session

时间:2015-10-26 23:00:01

标签: java spring hibernate

我正在尝试使用Hibernate版本4.3.11.Final和spring版本4.2.2.RELEASE。 当我在UserDAO中获得会话时,我有一个hibernate异常:找不到当前线程的会话。我不明白为什么你能帮帮我吗?

在我的文件和来源下面:

谢谢

的web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml,
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Spring Security -->  
 <filter>  
  <filter-name>springSecurityFilterChain</filter-name>  
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
 </filter>  

 <filter-mapping>  
  <filter-name>springSecurityFilterChain</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  

根context.xml中

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url"
        value="jdbc:mysql://localhost:8889/flosite" />
    <property name="username" value="test" />
    <property name="password" value="" />
</bean>

<!-- Hibernate Session Factory -->


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan">
        <array>
            <value>com.rg.flosite.model</value>
            <value>com.rg.flosite.dao</value>
            <value>com.rg.flosite.service</value>
        </array>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQLDialect
        </value>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="myDataSource"/>
      <property name="sessionFactory" ref="sessionFactory"/>
</bean> 
<tx:annotation-driven transaction-manager="transactionManager"/>

servlet的context.xml中

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles.xml</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="preparerFactoryClass"
        value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory" />
</beans:bean>

<beans:bean id="userService" class="com.rg.flosite.service.UserServiceImpl"></beans:bean>
<beans:bean id="userDao" class="com.rg.flosite.dao.UserDAOImpl"></beans:bean>
<context:component-scan base-package="com.rg.flosite.controller" />

userDao.java

package com.rg.flosite.dao;

import com.rg.flosite.model.User;

public interface UserDAO {
    public void updateUser(User user);
    public User getUser();
}  

UserDAOImpl.java

package com.rg.flosite.dao;

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

import com.rg.flosite.model.User;

@Repository
public class UserDAOImpl implements UserDAO {
    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void updateUser(User user) {
        User userToUpdate = getUser();
        if (userToUpdate == null) {
            user.setId((long) 1);
            getCurrentSession().save(user);
        }
        else {
            user.setId(userToUpdate.getId());
            getCurrentSession().update(user);
        }
    }

    @Override
    public User getUser() {
        User user = (User) getCurrentSession().get(User.class,1);
        return user;
    }

}

UserService.java

package com.rg.flosite.service;

import com.rg.flosite.model.User;

public interface UserService {
    public User getUser();
    public void updateUser(User user);

}

UserServiceImpl.java

package com.rg.flosite.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rg.flosite.dao.UserDAO;
import com.rg.flosite.model.User;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDao;

    @Override
    public User getUser() {
        return userDao.getUser();
    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

}

AdminMeControler.java

package com.rg.flosite.controller;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.rg.flosite.model.User;
import com.rg.flosite.service.UserService;

@Controller
public class AdminMeController {
    @Autowired
    private UserService userService;
    @RequestMapping(value="/admin/moi", method = RequestMethod.GET) 
    public String home(Locale locale,Model model) {
        User user = userService.getUser();
        return "adminme";
    }

}

1 个答案:

答案 0 :(得分:0)

好的,我在其他论坛上找到了解决方案。 我的包DAO,模型,服务必须由应用程序上下文(root-context.xml)扫描,而不是由servlet上下文(servlet-context.xml)扫描。 所以我将下面的代码从servlet-context更改为root-context。

<bean id="userService" class="com.rg.flosite.service.UserServiceImpl"></bean>
<bean id="userDao" class="com.rg.flosite.dao.UserDAOImpl"></bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.rg.flosite.dao" />
<context:component-scan base-package="com.rg.flosite.model" />
<context:component-scan base-package="com.rg.flosite.service" />