我有一个与hibernate一起工作的spring应用程序。hibernate的会话没有创建。它抛出如下错误
[控制台错误][1]:http://i.stack.imgur.com/mb0ah.png
我的调度员代码如下
<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.oi.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- data source -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:61391/springmvc2k" />
<property name="username" value="adminHYMgZHE" />
<property name="password" value="byIkcunaje5K" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.oi.bean"></property>
</bean>
<!-- The transaction manager -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
我的豆子课
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Entity;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue
int id;
@Column(name = "username")
String name;
@Column(name = "password")
String password;
@Column(name = "email")
String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
我的Spring控制器如下
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
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 com.oi.bean.User;
@Controller
public class BaseController {
@Autowired
private SessionFactory sessionFactory;
@RequestMapping(value="/")
public String getRespnse(Model m) {
Session session =sessionFactory.getCurrentSession();
// List<User> usrLst=(List<User>) session.createQuery("from User");
// System.out.println("IN"+usrLst);
return "home";
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
请帮助。如何继续。。!!!
3条答案
按热度按时间vd8tlhqk1#
使用控制器方法添加@transactional
删除@enabletransactionmanagement到用户类
试试看,我想对你有用
drkbr07n2#
添加
@Transactional
属性getRespnse(Model m)
在你的BaseController
下课并尝试以下内容。pbpqsu0x3#
在我的例子中,我在整个控制器中添加了@transactional注解,并且成功了