hibernate 在做更新和删除的时候一定要把事务开启

在做更新和删除的时候一定要把事务开启

在做更新和删除的时候一定要把事务开启

在做更新和删除的时候一定要把事务开启

重要的事情说三遍!!!

curd之前配置文件

<property name="hbm2ddl.auto">update</property>

练习hibernate的CURD(单表操作 save& update& delete& get/load )时,发现update&  delete方法执行不成功,冥思苦想也没想出个所以然,期间连重启等等笨办法都试了,结果毫无头绪,等到不经意间往上一翻,发现TM之前试验不利用事务提交的方法后事务没开启,WTF。。。

回顾一下不开事务也能提交的方法

session.doWork(new Work(){

            @Override
            public void execute(Connection connection) throws SQLException {
                // TODO Auto-generated method stub
                connection.setAutoCommit(true);
            }
            
        });
        //保存对象进数据库
        session.save(s);
        //强制输出sql语句
        session.flush();

整个代码。。。之前init()和destory()中红色部分注释了

package hibernate_01;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

//测试的源程序要写在新建的Source Folder里 

//测试类
public class StudentsTest {

    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;
    
    @Before
    public void init(){
        //创建配置对象
        Configuration config =new Configuration().configure();
        //创建服务注册对象
        ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        //创建会话工厂对象
        sessionFactory=config.buildSessionFactory(serviceRegistry);
        //会话对象
        session =sessionFactory.openSession();
        //开启事务
        transaction =session.beginTransaction();
        
        
    }
    
    @After
    public void destory(){
        transaction.commit();//提交事务
        session.close();//关闭会话
        sessionFactory.close();//关闭会话工厂
    }
    
    
    @Test
    public void testSaveStudents(){
        
        //生成学生对象
        Students s=new Students(2,"张三","男",new Date(),"山东");
        
        session.doWork(new Work(){

            @Override
            public void execute(Connection connection) throws SQLException {
                // TODO Auto-generated method stub
                connection.setAutoCommit(true);
            }
            
        });
        //保存对象进数据库
        session.save(s);
        //强制输出sql语句
        session.flush();
    }
        
    @Test
    public void testGetStudents(){
        Students s=(Students) session.get(Students.class, 1);  //.get(查询表对应的类对象, 查询对象的主键);
        System.out.println(s.toString());
        
    }
    
    @Test
    public void testLoadStudents(){
        Students s=(Students) session.load(Students.class, 1);  //.load(查询表对应的类对象, 查询对象的主键);
        System.out.println(s.toString());
        
    }
    
    @Test
    public void testUpdateStudents(){
        Students s=(Students) session.get(Students.class, 1);  
        s.setGender("女");
        session.update(s);
        session.flush();
    }
    
    @Test
    public void testDeleteStudents(){
        Students s=(Students) session.load(Students.class, 2);  
        System.out.println(s.toString());
        session.delete(s);
        session.flush();
    }  
      
}

当然,不开启事务,也能执行成功(虽然麻烦),update  &delete 都要学习testSaveStudents()方法添加doWork那一段了

原文地址:https://www.cnblogs.com/zjfjava/p/6593398.html