gpt4 book ai didi

java - Hibernate 4 : org. hibernate.HibernateException: 没有找到当前线程的 session

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:05 26 4
gpt4 key购买 nike

我正在尝试使用 Hibernate 版本 4.3.11.Final 和 spring 版本 4.2.2.RELEASE。当我在 UserDAO 中获取 session 时,出现 hibernate 异常:未找到当前线程的 session 。我不明白为什么你能帮我?

下面是我的文件和来源:

谢谢

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>

root-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";
}

}

最佳答案

好的,我在其他论坛上找到了解决方案。我的包 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" />

关于java - Hibernate 4 : org. hibernate.HibernateException: 没有找到当前线程的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33357087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com