java - Hibernate异常: saveOrUpdate is not valid without active transaction

标签 java hibernate spring-mvc transactions

也许这个问题已被多次提出,但我无法找到解决方案。我是 Spring 新手,正在尝试开发一个简单的项目,以使用注释将 Spring 与 Hibernate 4 集成。每当我单击用户表单保存到数据库时,它都会抛出此异常:

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction

我做错了什么?我的代码如下:

用户.java

    @Entity
    @Table(name="SpringUsers")

    public class User implements Serializable{
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "userId")
        private int id;

        @Column(name = "username")
        private String username;


         @Column(name = "password")
         private String password;

        @Column(name = "email")
        private String email;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

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

UsDAOImpl.java

    @Repository


    public class UserDAOImpl implements UserDAO{
        @Autowired
        private SessionFactory sessionFactory;
            public UserDAOImpl(){}

        public UserDAOImpl( SessionFactory sessionFactory){
            this.sessionFactory= sessionFactory;
        }

       @Override
       @Transactional
        public List<User> list() {
           List<User> listUser = sessionFactory.getCurrentSession()
                  .createCriteria(User.class).list();
           return listUser;
        }

        @Override
        @Transactional
        public void saveOrUpdate(User user) {
           sessionFactory.getCurrentSession().saveOrUpdate(user);
        }

        @Override
        @Transactional
        public void delete(int id) {
            User userToDelete = new User();
            userToDelete.setId(id);
            sessionFactory.getCurrentSession().delete(userToDelete);
        }

        @Override
        @Transactional
        public User get(int i) {
            String hql = "from User where id=" + i;
            Query query = sessionFactory.getCurrentSession().createQuery(hql);
            List<User> listUser = (List<User>) query.list();

            if (listUser != null && !listUser.isEmpty()) {
                return listUser.get(0);
            }

            return null;
       }
      }

用户-servlet.xml

    <context:component-scan base-package="com.myspringapp.controller"/>
        <mvc:annotation-driven/>
        <context:annotation-config />


        <!--       <tx:annotation-driven />-->


        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
            <property name="username" value="system"/>
            <property name="password" value="henry"/>
        </bean>

        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>


            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.current_session_context_class">thread</prop>
                </props>
            </property>
            <property name="annotatedClasses">
                <list>
                    <value>com.myspringapp.model.User</value>
                </list>
            </property>
        </bean>

       <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <constructor-arg  ref="sessionFactory"/>
        </bean>
        <bean id="userDao" class="com.myspringapp.dao.UserDAOImpl">
            <constructor-arg ref="sessionFactory"/>        
        </bean>

    <!--   <tx:annotation-driven transaction-manager="transactionManager"/>-->


        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
</bean> 

最佳答案

取消注释此行,它应该可以工作:

<tx:annotation-driven />

关于java - Hibernate异常: saveOrUpdate is not valid without active transaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32175749/

相关文章:

java - CronTrigger 未在指定时间触发,而是在错误的时间发送多个推送通知

java - 外键列被插入为空

java - IIS 重写 BadRequest 响应数据

java - Spring Boot 多数据源

java - 限制迭代时删除元素

java - 键盘轮询系统

java - 通过 jpa 将两个表连接到一个 java 对象中

java - Hibernate @OneToOne 和 ConstraintViolationException 关系的一侧

java - 如何在spring mvc中删除session属性

java - Eclipse 代码模板