簡單的 Hibernate Test

Test.java

 

import org.hibernate.cfg.*;

import org.hibernate.*;

 

public class Test {

   public static void main(String[] args) throws Exception {

      Student student = new Student();

      student.setName("Jack");

      student.set...;// 設置student 對象

 

      Configuration config = new Configuration();// 1. Configuration類,讀配置文件和映射文件

      //read hibernate.cfg.xml and hbm.xml

      config = config.configure();

 

      SessionFactory sf = config.buildSessionFactory();// 2. SessionFactory 工廠類 接口

      Session session = sf.openSession();// 3. Session 核心

 

      Transaction trans = session.beginTransaction();

 

      session.save(student);

      //User user = session.load(User.class,new Long(1001));

      //session.update(user);

      //session.delete(user);

 

      trans.commit();

      session.close();

   }

}

發佈了36 篇原創文章 · 獲贊 6 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章