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();
 }

}

 

 

posted @ 2012-04-18 14:48  慢吞吞  阅读(4909)  评论(0编辑  收藏  举报