我已阅读并找到类似问题的很多答案,例如
org.hibernate.HibernateException: No Session found for current thread
我试图添加tx:annotation-driven transaction-manager =" transactionManager"> ** 但结果我有错误 HTTP状态500 - servlet mvc-dispatcher的Servlet.init()抛出异常
我该如何解决这个问题。 (Spring 3.2.0 + Hybernate 4.2.0.Final)
的信息。
web.xml
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispercher-servlet.xml中
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.springapp.mvc"/>
<context:annotation-config/>
<tx:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan"
value="com.springapp.mvc.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test_student" />
<property name="username" value="root" />
<property name="password" value="181987" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
HibernateSpitterDao
@Repository 公共类HibernateSpitterDao {
private SessionFactory sessionFactory;
@Autowired
public HibernateSpitterDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory; // Конструирует DAO
}
@Transactional(readOnly = false)
public void savestudent(Student student) {
Session currentSession = sessionFactory.getCurrentSession();
Transaction transaction = currentSession.beginTransaction();
currentSession.save(student);
transaction.commit();
// Использует текущий сеанс
}
}
控制器
@Controller
@RequestMapping("/")
public class HelloController {
private HibernateSpitterDao hibernateSpitterDao;
@Autowired
public HelloController(HibernateSpitterDao hibernateSpitterDao) {
this.hibernateSpitterDao = hibernateSpitterDao;
}
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
model.addAttribute("Student",new Student());
return "hello";
}
@RequestMapping(method = RequestMethod.POST)
public String printWelcome1(@Valid Student student,
BindingResult bindingResult) {
hibernateSpitterDao.savestudent(student);
return "hello";
}
}
JSP
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<body>
<h1>${message}</h1>
<h3><strong>Start</strong></h3>
<div>
<sf:form method="post" modelAttribute="Student">
<label for="login1"> Login </label>
<sf:input path="name" id="login1" />
<p> </p>
<label for="pass"> password </label>
<sf:password path="password" id="pass" />
<p></p>
<button type="submit">Registration</button>
</sf:form>
</div>
</body>
答案 0 :(得分:2)
您需要更新dispercher-servlet.xml
对会话上下文使用以下值。
<prop key="hibernate.current_session_context_class">thread</prop>
线程是org.hibernate.context.internal.ThreadLocalSessionContext
请查看此链接以获取更多详细信息。
http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#architecture-current-session
答案 1 :(得分:1)
你的saveStudent dao方法上有@Transactional注释,但是也可以尝试在你的dao方法中手动创建一个事务,做一个或者其他如下:
@Transactional(readOnly = false)
public void savestudent(Student student) {
Session currentSession = sessionFactory.getCurrentSession();
currentSession.save(student);
}
另外,请确保您的dao类位于component-scan标记中指定的包的内部或下方,否则spring将无法获取注释。或者,您可以在弹簧布线中为您的dao添加bean定义,这也将允许spring对其注释执行某些操作。
答案 2 :(得分:1)
我遇到了同样的问题。
我使用的是基于注释的配置,我已经配置了sessionfactory,datasource和事务管理器。但是我没有在AppConfig类中给出@EnableTransactionManagement注释。
添加事务批注后,代码如下所示。
@Configuration
@ComponentScan("com.bmp.*")
@EnableWebMvc
@PropertySource("classpath:${env}.properties")
@EnableTransactionManagement
public class AppConfig {
-----
}
以上注释解决了我的问题。
答案 3 :(得分:0)
解 1.删除Repository类中的Constructor。 2.删除Repository类中的事务。
答案 4 :(得分:0)
请在dao层使用@Repository批注,在保存学生的顶部使用@Transnational