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那一段了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM