Hibernate系列4-----之刪除

來源:互聯網
上載者:User

標籤:onclick   display   知識點   nfa   gif   .config   pen   factory   返回   

1.和它的增改查兄弟不同,多了個until包定義了HibernateUntil類,讓我們來一起看看吧

 1 public class HibernateUntil { 2     private static Configuration cfg=new Configuration().configure(); 3    private static SessionFactory factory=cfg.buildSessionFactory(); 4  5     //1.方法返回session  靜態成員變數不能直接使用非靜態成員 6     //Session依賴於Session工廠,工廠依賴於Configure 7     public static Session getSession(){ 8       return factory.openSession(); 9 10     }11     //2.關閉session12     public static void closeSession(){13         getSession().close();14     }
View Code

這裡也考察了一個知識點靜態變數與非靜態變數    要想學好就得把基礎打好!

2.接下來我們來寫一下測試類別

1 @Test2     public void testhibernate() {3         deleteStudent();//刪除學生4     }
View Code
 1  private void deleteStudent() { 2       Session session= HibernateUntil.getSession(); 3       Student student=new Student(); 4       student.setSid(2); 5       //開啟事務 6       Transaction tx=session.beginTransaction(); 7       session.delete(student); 8       tx.commit(); 9       HibernateUntil.closeSession();10         System.out.println("success  ok");11     }
View Code

 

恩,Hibernate系列增刪改查就此結束,感謝同行們的閱覽哈!

 

Hibernate系列4-----之刪除

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.