hibernate Connection cannot be null when 'hibernate.dialect' not set


是測試CURD的地方配置sessionfactory有錯誤

下面是兩種寫法,第二種的寫法已經過時,不要缺少下面紅色字體代碼

<1>

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Teacher t = new Teacher();
  //student.setId(1);
  t.setId(4);
  t.setName("cc");
  t.setTitle("高級");
  Configuration cg = new Configuration();
  cg.configure();//不要缺少或寫在下面,這個是configure和serviceregistry之間
  ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cg.getProperties()).buildServiceRegistry();
  SessionFactory sf = cg.buildSessionFactory(sr);
  Session s = sf.openSession();
  s.beginTransaction();
  s.save(t);
  s.getTransaction().commit();
  s.close();
  sf.close();
 }

}

<2>

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Student student = new Student();
  //student.setId(1);
  student.setName("小李");
  student.setAge(32);
  Configuration cg = new Configuration();
  SessionFactory sf =  cg.configure().buildSessionFactory();
  Session s = sf.openSession();
  s.beginTransaction();
  s.save(student);
  s.getTransaction().commit();
  s.close();
  sf.close();
 }

}

 

 


免責聲明!

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



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