hibernate 在做更新和刪除的時候一定要把事務開啟

來源:互聯網
上載者:User

標籤:on()   cto   學習   類對象   except   測試   語句   registry   tin   

在做更新和刪除的時候一定要把事務開啟

在做更新和刪除的時候一定要把事務開啟

在做更新和刪除的時候一定要把事務開啟

重要的事情說三遍!!!

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

 

hibernate 在做更新和刪除的時候一定要把事務開啟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.